react-native - 使用反应原生导航从父级访问状态值
问题描述
我正在像这样使用 createStackNavigator:
navigationOptions: ({ navigation }) => {
return {
title: 'Account Details',
headerLeft : <Back nav={ navigation } goto={'Profile'}/>,
headerRight: <Icon
name='check'
color='#fff'
onPress={() => **SOME FUNCTION** }
/>
}
}
此标题加载在“帐户详细信息”表单中,他们可以在其中编辑其用户名等。我想使用标题中的“检查”图标来保存输入的信息。
有没有办法告诉它使用我的“帐户详细信息”组件中存在的函数,以便我可以从那里访问状态值?或者以某种方式将它们传回给父母?
提前致谢。
解决方案
将该功能传递给state
ofnavigation
并正常使用它
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() }
/>
...
}
}
推荐阅读
- python - 为什么在这种情况下,python 的 strip 和 lstrip 方法的行为不符合预期?
- javascript - Slack 为用户请求权限
- javascript - 如何获取 32 位整数的位长度
- javascript - 如何在测试脚本中动态更新 ResponseBody?
- javascript - 在 Node-RED 的函数节点中使用 javascript 内置函数
- firefox - 如何创建更改新标签页行为的 Firefox 扩展程序?
- dart - 小部件库在颤动中捕获的异常
- python - 未按预期获得 XML 输出
- java - 调用方法时参数与方法类中的格式完全相同时不兼容
- linux - 在 linux 服务器上找不到 wget 命令