我正在使用react-native和redux。我正在尝试更新当前屏幕的参数,以便可以在顶栏中使用的组件中访问它们,但参数未设置。我的代码如下:
屏幕路线:
AlertNameForm: {
screen: AlertNameForm,
navigationOptions: ({navigation}) => CancelAndDone(navigation)
}
组件屏幕:在componentDidMount中我正在设置参数。
class AlertNameForm {
..........
componentDidMount() {
this.props.navigation.setParams({onDonePress: this.onDonePress})
}
onDonePress: () => {
// want to access this function in top-bar buttons.
}
}
以下是其他组成部分:
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())
}
}} />
))
但是在ButtonDone组件中,我无法访问onDonePress函数。在react-native中,还有其他任何方法可以将setParams用于当前屏幕。
你应该参考navigation.state.params
using this.props
,因为导航应该作为prop传递给那个组件。