setParams无法在本机中工作

Adn*_*Ali 5 reactjs react-router react-native react-redux react-navigation

我在redux中使用react-native。我正在尝试更新当前屏幕的参数,以便可以在顶栏中使用的组件中访问它们,但未设置参数。我的代码如下:

屏幕路线:

AlertNameForm: {
    screen: AlertNameForm,
    navigationOptions: ({navigation}) => CancelAndDone(navigation)
  }
Run Code Online (Sandbox Code Playgroud)

组件屏幕:在componentDidMount中,我正在设置参数。

class AlertNameForm {
    ..........
    componentDidMount() {
    this.props.navigation.setParams({onDonePress: this.onDonePress})
    }

    onDonePress: () => {
      // want to access this function in top-bar buttons.
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是其他组件:

export const CancelAndDone = (navigation) => ({
    headerLeft: <ButtonCancel navigation={navigation} />,
    headerRight: <ButtonDone navigation={navigation} />
})

const ButtonDone = withTheme(({navigation, theme: { tertiaryColor } }) => (
  <Button color={tertiaryColor} title="Done" onPress={() => {
      if (navigation.state.params && navigation.state.params.onDonePress) {
        navigation.state.params.onDonePress()
      }
      else {
        navigation.dispatch(NavigationActions.back())
      }
    }} />
))
Run Code Online (Sandbox Code Playgroud)

但是在ButtonDone组件中,我无法访问onDonePress函数 是否有其他方法可以在react-native中设置当前屏幕的Param

Ner*_*gen 0

您应该引用navigation.state.paramsusing ,this.props因为导航应该作为 prop 传递给该组件。