1

我想在单击按钮时将数据传递到另一个屏幕。我使用的是 router-flux,它工作得很好,但是在 react-navigation 上它不起作用。因此,在 router-flux 上,单击按钮时,它会调用此函数:

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

但是在反应导航中,如果我将代码更改为:

  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 Cars!”。我究竟做错了什么?在 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>
4

1 回答 1

0

尝试将您的更改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!");
      });
  }
于 2017-04-18T15:15:00.910 回答