首页 > 解决方案 > 使用反应原生导航从父级访问状态值

问题描述

我正在像这样使用 createStackNavigator:

navigationOptions: ({ navigation }) => {
    return {
        title: 'Account Details',
        headerLeft : <Back nav={ navigation } goto={'Profile'}/>,
        headerRight: <Icon
            name='check'
            color='#fff'
            onPress={() => **SOME FUNCTION** }
        />
    }
}

此标题加载在“帐户详细信息”表单中,他们可以在其中编辑其用户名等。我想使用标题中的“检查”图标来保存输入的信息。

有没有办法告诉它使用我的“帐户详细信息”组件中存在的函数,以便我可以从那里访问状态值?或者以某种方式将它们传回给父母?

提前致谢。

标签: react-nativeexporeact-native-navigation

解决方案


将该功能传递给stateofnavigation并正常使用它

class YourComponent extends Component {
  componentDidMount(){
    this.props.navigation.setParams({
      yourFuntion: this._yourfunction,
    })
  }
  
  yourfunction = () => {
    // handle thing with state here
  }
}

/////////////////////////////////////////////////

navigationOptions: ({ navigation }) => {
    const yourFuntion = navigation.getParam("yourFuntion", ()=>{});
    return {
        ...
        headerRight: <Icon
            name='check'
            color='#fff'
            onPress={() => yourFuntion() }
        />
        ...
    }
}


推荐阅读