hel*_*eer 2 javascript react-native react-navigation
我将我的整数数组设置为selectedStyleIds。为什么我的this.props.navigaion.setParams无法正常工作?
_setSelectedStyleIds = (selectedStyleIds) => {
const action = NavigationActions.setParams({
params: {selectedStyleIds},
key: 'id-1509842157447-6' <- got this from console
});
this.props.navigation.dispatch(action);
this.props.navigation.setParams({styleIds:selectedStyleIds});
this.setState({selectedStyleIds});
}
onCheck = ({selectedStyleIds}) => {
console.log('onCheck');
console.log(selectedStyleIds); <- [1,2,3,4,5]
this.props._setSelectedStyleIds(selectedStyleIds);
this.setState({selectedStyleIds}); <- working
}
_handleTagStylePress = () => {
this.props.navigation.navigate('TagStyle', {onCheck: this.onCheck, selectedStyleIds: this.state.selectedStyleIds});
}
Run Code Online (Sandbox Code Playgroud)
在onNavigatioOptions上(仅用于调试)
onPress={() => {
let {image, text, ..., selectedSeasonIds,
gender, selectedSizeIds, selectedColorIds, selectedStyleIds,
brand, ..., base64 } = navigation.state.params;
console.log('onsave')
console.log(navigation.state.params.selectedStyleIds) <<-- []
Run Code Online (Sandbox Code Playgroud)
我已经检查了这些行超过50次,但显然this.props.navigation.setParams({selectedStyleIds});无法正常工作。
其他所有设置状态和参数都没有问题,但只能将selectedStyleIds设置为navigation.state.params。
关于从组件本身设置导航栏的标题,我也遇到类似的问题。
以下对我有用。
1)我定义了navigationOptions方法(反应导航的回调方法来设置导航属性)
当此方法运行时,您很可能无法访问设置导航器状态所需的属性,因此只需返回当前参数,这就是我所做的:
static navigationOptions = ({ navigation }) => {
const { params } = navigation.state;
return params;
};
Run Code Online (Sandbox Code Playgroud)
2)我在componentDidMount中实现了标题构造,设置了状态:
componentDidMount(){
t = "The window title";
this.props.navigation.setParams({title: t });
}
I think the same could be done in any user interface event, like the onPress, and can be applied to any property.
Run Code Online (Sandbox Code Playgroud)
重要提示:做的差我之间有什么不工作而工作是navigationOptions方法的定义,即使它是一个“什么也不做”的方法(返回它所得到维持不变)。
| 归档时间: |
|
| 查看次数: |
9002 次 |
| 最近记录: |