我想在单击按钮时将数据传递到另一个屏幕。我正在使用 router-flux 并且它工作得很好,但是在 react 导航上它不起作用。 因此,在 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 导航上,如果我将代码更改为:
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>
最佳答案
尝试将 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!");
});
}
关于javascript - React Native 通过react-navigation传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43475448/