【问题标题】:Access state values from parent using react native navigation使用反应原生导航从父级访问状态值
【发布时间】:2018-08-20 02:40:22
【问题描述】:

我正在像这样使用 createStackNavigator:

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

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

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

提前致谢。

【问题讨论】:

    标签: react-native expo react-native-navigation


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-10-05
      • 2018-07-11
      • 1970-01-01
      • 2021-02-22
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      相关资源
      最近更新 更多