在异步功能中导航-React导航-React Native

时间:2020-03-06 09:40:42

标签: javascript reactjs react-native ecmascript-6 react-navigation

我试图在ReactNative中从一个组件导航到另一个屏幕。但要关注

可能的未处理的承诺拒绝,TypeError:未定义不是对象(正在评估“ navigation.navigate”)]

以下是我的代码,我在其中等待获取位置详细信息,然后想导航到包含这些详细信息的另一个屏幕。

constructor(props) {
super(props)
this.state = {status: 'initial'}
this._handlePress = this._handlePress.bind(this);
 }



_handlePress = async ({navigation}) => {
const res = await this.props.fetchDetails(this.props.place_id)
console.log('result', res.geometry.location.lat)
navigation.navigate("Home", {selectedLocation: res.geometry.location});

}

我也尝试过this.props.navigation.navigate(),但无法解决。

我得到以下消息:

[未处理的承诺拒绝:TypeError:未定义不是对象(正在评估'_this.props.navigation.navigate')]

以下为完整的组件代码:Pastebin Link

1 个答案:

答案 0 :(得分:1)

使用withNavigation方法,因为您已经从react-navigation导入了它。

export default withNavigation(LocationItem);

如果未解决,则从使用它的地方通过导航作为道具。

<LocationItem navigation={this.props.navigation}/>
相关问题