使用导航参数设置Redux状态

时间:2018-09-27 15:12:17

标签: react-native react-redux react-navigation

在组件之外,我有redux片段,在这些片段中,我试图通过导航传递一些参数并将其设置为initialState。但是,它返回错误“无法读取导航的属性”:

const initialState = this.props.navigation.setParam('initialState, {});

const reducer = (state = initialState, action) => {
    switch (action.type) {
        case 'UPDATE_BASE_PREMIUM':
            return { ...state, basePremium: action.payload / 100 }
        case 'UPDATE_TOTAL_PREMIUM':
            return { ...state, totalPremium: action.payload / 100 }
        case 'CHANGE_MODE_OF_PAYMENT':
            return { ...state, modeOfPayment: action.payload }
        case 'CHANGE_FIRST_NAME':
            return { ...state, firstName: action.payload }
        case 'CHANGE_LAST_NAME':
            return { ...state, lastName: action.payload }
        case 'CHANGE_DOB':

如果我运行'this.props.navigation.setParam('initialState,{});'在组件本身中,然后它可以完美运行,但是在这种情况下,initialState仍然不会更改:

export default class SavedIllustrations extends Component {
    constructor(props) {
        super(props);
        this.state = {
        }
    }

    static navigationOptions = {
        header: null
    };

    componentDidMount() {
        initialState = this.props.navigation.getParam('initialState', {});
        console.log(initialState);
    }

    render() {
        return (
            <Provider store={store}>
                <ScrollView contentContainerStyle={styles.container}>
                    <HeaderLogo />
                    <NewSaveIllustration navigation={this.props.navigation} />
                    <ReviewSavePrint />
                    <Calculator />
                    <InsuredInformation />
                    <BasePlan />
                    <TermRiders />
                    <OptionalBenefitRiders />
                </ScrollView>
            </Provider>
        );
    }
}

通过导航参数导入initialState的最佳方法是什么?

0 个答案:

没有答案