首页 > 解决方案 > 如何在 React Native 中通过 this.props.navigation.openDrawer() 发送参数

问题描述

我是反应原生的新手。我想通过 this.props.navigation.openDrawer() 函数在 react native 中发送参数。这是我的代码:但是,不起作用。

refreshSettingView() {
    console.log('refresh data setting')
  }

render() {
return(
     <TouchableOpacity
        onPress={() => {
         this.props.navigation.openDrawer(this.refreshSettingView.bind(this))
        }
    }>
         <Text>Button Open</Text
     </TouchableOpacity>
    )
}

标签: reactjsreact-native

解决方案


在您的抽屉组件中,尝试:

componentWillReceiveProps = (props) => {
    if(!props.navigation.state.isDrawerOpen){
         // Refresh the page here 
    }
}

componentWillReceiveProps每次传递给传递的道具发生变化时,都会调用该方法。componentWillReceiveProps可以在这里找到完整的参考: https ://reactjs.org/docs/react-component.html#updating-componentwillreceiveprops

让我知道它是否有效!谢谢。


推荐阅读