首页 > 解决方案 > this.props.navigation.setParams() 不会更新之前传递的导航参数

问题描述

我在我的 react-native 项目中使用 a ,并分别TabNavigator通过 和 在屏幕之间传递一些数据。问题是这种方法对我有用,仅当我第一次发送该数据时,第二次尝试发送数据时,离开屏幕时数据不会更新this.props.navigation.setParams()this.props.navigation.getParam()navigation.state.params

下面是负责接收和发送数据的代码片段

import {NavigationEvents} from 'react-navigation';

...

receiveData = () => {
    if (this.props.navigation.state.params.data !== null) {
        const data = this.props.navigation.getParam('data');
        this.setState({data});
    }
}

sendData = () => {
    const {data} = this.state;
    this.props.navigation.setParams({data});
}

render(){

...

<NavigationEvents
    onWillFocus = {this.receiveData}
    on WillBlur = {this.sendData}
/>

...

}

标签: react-nativecomponentsreact-navigationreact-componentreact-native-tabnavigator

解决方案


检查 tabnavigator 是否正确模糊/聚焦您的选项卡。


推荐阅读