繁体   English   中英

使用react-navigation反应Native传递数据

[英]React Native pass data with react-navigation

我想在点击按钮时将数据传递到另一个屏幕。 我正在使用router-flux,它运行得很好,但是在反应导航上它不起作用。 因此,在router-flux上,点击按钮时,它会调用此函数:

  onSearch() {
    fetch(`URL`, {
        method: 'GET',
        })
      .then((response) => { return response.json() } )
      .then((responseJson) => {
        Action.results({data: responseJson});
      })
      .catch((error) => {
        Alert.alert("0 Cars!");
      });
  }

但是在react-navigation上,如果我将代码更改为:

  onSearch() {
    fetch(`URL`, {
        method: 'GET',
        })
      .then((response) => { return response.json() } )
      .then((responseJson) => {
        this.props.navigation.navigate('Resultados', { data: responseJson });
      })
      .catch((error) => {
        Alert.alert("0 Cars!");
      });
  }

它不起作用,我总是得到警报“0汽车!”。 我究竟做错了什么? 在router-flux上它非常简单。

这就是我调用onSearch函数的方式:

    <Button onPress={this.onSearch} style={{backgroundColor: '#f48529', width: 140, borderRadius: 30, height: 40}} >
        <Text style={{color: 'white', fontFamily: 'rubik-light', fontSize: 17}}>Search</Text>
        <Icon size={15} name={'search'} color={'white'} />
    </Button>

尝试将onPress={this.onSearch}更改为onPress={this.onSearch.bind(this)}

并且

尝试

onSearch() {

    const { navigate } = this.props.navigation;

    fetch(`URL`, {
        method: 'GET',
        })
      .then((response) => { return response.json() } )
      .then((responseJson) => {
        navigate('Resultados', { data: responseJson });
      })
      .catch((error) => {
        Alert.alert("0 Cars!");
      });
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM