首页 文章

如何在react-navigation中将变量作为const变量传递

提问于
浏览
0

在我的应用程序中使用react-navigation时,我需要传递一些被认为是目标屏幕的const变量 .

因为我们使用react-navigation传递到目标屏幕的所有参数都是通过像 this.props.navigation.state.params.xxx 这样的东西来访问的,我想知道如何 mark 这个变量的一些"const"就像我们用 props 初始化一个组件一样?

将此const变量存储在 this.state 中是非常令人困惑的,尽管这是一个可行的选择,因为不应在屏幕内更改此变量 .


更新:

抱歉我的表情令人困惑 .

核心问题是 how to mark variable passed by react-navigation as a const one .

我的意思是,我有一个名为'MainScreen'的屏幕类,现在我想通过react-navigation导航到名为'SecondaryScreen'的另一个屏幕 . 所以我写这样的代码:

const {navigate} = this.props.navigation;
navigate('SecondaryScreen', {
    agentCode: this.state.agentInfo.agentCode,
    queryDate: '20180101'
})

我希望agentCode是SecondaryScreen中的const,queryDate是一个变量 .

所以对于SecondaryScreen中的queryDate我写这个:

constructor(props: any) {
    super(props)
    var params = this.props.navigation.state.params
    this.state = {
        queryDate = params.queryDate
    }
}

现在queryDate存储正确 . 但是agentCode怎么样?

如何将 this.props.navigation.state.params.agentCode 存储为const类变量?

如果我自己启动SecondaryScreen,我可以使用

<Secondary agentCode={this.state.agentInfo.agentCode}>

通过这样做,可以通过辅助中的 this.props.agentCode 访问agentCode . 但是使用react-navigation,我不知道如何实现相同的效果 .

由于反应中的所有类变量都存储在props或state中,我想知道如何通过react-navigation将const传递给SecondaryScreen?

1 回答

  • 0

    您可以在组件安装时将其分配给const .

    componentDidMount(){
       const agentCode = this.props.navigation.state.params.agentCode;
    }
    

相关问题