React Native atob() / btoa() not working without remote JS debugging

前端 未结 3 761
灰色年华
灰色年华 2020-12-04 15:18

I have a testing app in react native, and all works fine when I have enabled the debug js remotely. It works fine in device (from XCode) and simulator, after run:

         


        
3条回答
  •  感动是毒
    2020-12-04 15:48

    That's the ways I fixed it. As @chemitaxis suggests, add base-64 module from NPM:

    npm i -S base-64
    

    Based on it, I propose a couple of ways to use it:

    Importing it in files you need it

    Then, you can import 'encode' and 'decode' methods using aliases, this way:

    import {decode as atob, encode as btoa} from 'base-64'
    

    Of course, using aliases is optional.

    Polyfill way

    You can set atob and btoa as global variables on React Native. Then, you won't need to import them on each file you need it. You have to add this code:

    import {decode, encode} from 'base-64'
    
    if (!global.btoa) {
        global.btoa = encode;
    }
    
    if (!global.atob) {
        global.atob = decode;
    }
    

    You need to place it at the beginning of your index.js, so that it can be loaded before another file uses atob and btoa.

    I suggest you to copy it on a separate file (let's say base64Polyfill.js), and then import it on index.js

提交回复
热议问题