我在react native中有一个测试应用程序,当我远程启用调试js时,一切都正常.它在设备(来自XCode)和模拟器中运行良好,运行后:

react-native run ios

问题是,如果我停止远程js调试,登录测试将不再有效.登录逻辑非常简单,我正在获取api以测试登录,api端点通过https.

我需要改变什么?

Updated: This code works perfetly with JS Debug Remote Enabled, if I disable it, it not works anymore.

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react'
import {
  AppRegistry,
  StyleSheet,      
  View,
  Button,
  Alert
} from 'react-native'

export default class MyClass extends Component {

  constructor (props) {
    super(props)
    this.testFetch = this.testFetch.bind(this)
  }

  async testFetch () {
    const email = 'email@example.com'
    const password = '123456'

    try {
      const response = await fetch('https://www.example.com/api/auth/login', {
        /* eslint no-undef: 0 */
        method: 'POST',
        headers: {
          'Accept': 'application/json' /* eslint quote-props: 0 */,
          'Content-Type': 'application/json',
          'Authorization': 'Basic ' + btoa(email + ':' + password)
        }

      })
      Alert.alert('Error fail!', 'Fail')
      console.log(response)
    } catch (error) {
      Alert.alert('Error response!', 'Ok')
    }
  }

  render () {
    return (
      <View style={styles.container}>            
        <Button
          onPress={this.testFetch}
          title="Test me!"

        />            
      </View>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF'
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5
  }
})

AppRegistry.registerComponent('testingReactNative', () => MyClass)

谢谢

推荐答案

给你(https://sketch.expo.io/BktW0xdje).创建一个单独的组件(例如Base64.js),将其导入即可使用.比如Base64.btoa('123');

// @flow
// Inspired by: https://github.com/davidchambers/Base64.js/blob/master/base64.js

const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
const Base64 = {
  btoa: (input:string = '')  => {
    let str = input;
    let output = '';

    for (let block = 0, charCode, i = 0, map = chars;
    str.charAt(i | 0) || (map = '=', i % 1);
    output += map.charAt(63 & block >> 8 - i % 1 * 8)) {

      charCode = str.charCodeAt(i += 3/4);

      if (charCode > 0xFF) {
        throw new Error("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.");
      }

      block = block << 8 | charCode;
    }

    return output;
  },

  atob: (input:string = '') => {
    let str = input.replace(/=+$/, '');
    let output = '';

    if (str.length % 4 == 1) {
      throw new Error("'atob' failed: The string to be decoded is not correctly encoded.");
    }
    for (let bc = 0, bs = 0, buffer, i = 0;
      buffer = str.charAt(i++);

      ~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
        bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0
    ) {
      buffer = chars.indexOf(buffer);
    }

    return output;
  }
};

export default Base64;

React-native相关问答推荐

如何更改分隔底部标签和屏幕内容的线的 colored颜色 ?React Native Tab导航器

ITMS—91053:缺少API声明—ReactNative

react 本机函数不通过usContext触发

如何根据设备主题更改React Native中状态栏的背景?

任务:react-native-async-storage_async-storage:generateReleaseRFile FAILED

导出命名空间应首先由 `@babel/plugin-proposal-export-namespace-from` 转换

找不到变量 atob

由于依赖关系,在 run-android 上构建失败

在 React Native 中计算两个 lat+lng 坐标之间的距离?

@react-navigation/stack 与 @react-navigation/native-stack 有什么区别?

使用 create-react-native-app 创建了一个应用程序,如何将其发布到 Google Play store ?

React Native:无法点击调试菜单

Fragment片段内的react-native

如何在react-native中设置 FlatList 的刷新指示器?

我可以使用 react native 开发 pwa

区别需要MainQueueSetup 和dispatch_get_main_queue?

如何使 react-native Picker 停留在新 Select 的选项?

React Native,更改 React Navigation 标题样式

将 async 和 await 与 export const 一起使用

使用行数react-native文本组件