我正在ios上开发本机应用程序。我想对导航标题后退按钮单击做一些自定义逻辑。我的router.js代码是:
export const ProjectStack = createStackNavigator({
Question:{
screen: Question,
navigationOptions: {
title: 'Frage',
headerLeft: <HeaderBackButton onPress={() => navigationOptions.state.handleSave()} />
},
},
});
在组件中,我正在这样做:
componentDidMount(){
this.props.navigation.setParams({ handleSave: this._saveDetails });
}
_saveDetails() {
console.log("Back Button Clicked");
}
当组件加载时,出现异常:
Invariant violation: setParams can not be called by root navigator
有什么问题,我找不到问题
答案 0 :(得分:1)
您只能从导航器的屏幕组件之一修改其导航选项。这同样适用于嵌套为屏幕的导航器。 (Documentation link)
正确的方法是
在组件中设置导航选项
class Filters extends React.Component {
static navigationOptions = ({ navigation }) => {
const handleSave = navigation.getParam("handleSave", () => {});
return {
title: 'Frage',
headerLeft: <HeaderBackButton onPress={handleSave} />
};
};
并在组件的componentDidMount中设置
componentDidMount(){
this.props.navigation.setParams({ handleSave: this._saveDetails });
}
_saveDetails = () => {
console.log("Back Button Clicked");
}