如何将文本数据从先前的活动发送到API

时间:2019-03-05 13:28:06

标签: javascript react-native

我是本机反应的新手。我想将我的数据(文本)发送到API。怎么做?

self

通过这种编码,我可以从FirstActivity获取数据,但是如何在第二个活动中将该数据发送到API?

1 个答案:

答案 0 :(得分:0)

您是否收到this.props.navigation.state.params.no中的数据?

如果是这样,您可以在componentDidMount中发送它,当您导航到第二页时可以触发它,也可以使用某些react-navigation lifecycles。例如,我将使用componentDidMount

 class SecondActivity extends Component{
         static navigationOptions =
            {
                title: "Second Activity"
            };
    componentDidMount(){  //this will trigger everytime the screen is mounted
       this.callAPI() /we call a function that handles the API call 
    }
    async callAPI(){ //is async because i like it 7u7
      var numberReceived = this.props.navigation.state.params.no
       var Result = await fetch ....... //call API here and use the number received variable in it
    }
    render() {
            return (
            <View style= {styles.MainContainer}>
                <Text style={styles.textStyle}>
                1 = {this.props.navigation.state.params.no}
                </Text>
            </View>
            )}
    }

实现实际上取决于您尝试使用的API。他们应该有一些文档来说明如何调用API。当然,您应该具有fetch API或xmlhttprequest API

的一些基本知识。