使用React Native导航从父级访问状态值 [英] Access state values from parent using react native navigation

查看:95
本文介绍了使用React Native导航从父级访问状态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在这样使用createStackNavigator:

I'm using createStackNavigator like this:

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

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

This header loads in the 'Account Details' form, where they can edit their username etc. I want to use the 'check' Icon in the header to save the information entered.

有没有办法告诉我它使用我的帐户详细信息"组件中存在的功能,以便可以从其中访问状态值?还是以某种方式将它们传给父母?

Is there a way I can tell it to use a function that exists inside my 'Account Details' component, so I can access the state values from in there? Or somehow pass them back to the parent?

谢谢.

推荐答案

将该函数传递给navigationstate并正常使用

Pass that function to state of navigation and use it normally

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() }
        />
        ...
    }
}

这篇关于使用React Native导航从父级访问状态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆