RN redux 第二个action触发时把第一个action更改过的props初始化的问题



  • import React, {Component} from 'react';
    import {Navigator, View, Text} from 'react-native';
    import {createStore, applyMiddleware, combineReducers} from 'redux';
    import {Provider} from 'react-redux';
    import thunk from 'redux-thunk';
    import * as reducers from '../reducers';
    import Discovergift from './index';
    const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
    const reducer = combineReducers(reducers);
    const store = createStoreWithMiddleware(reducer);

    export default class App extends Component {

    constructor(props) {
        super(props);
        this.state = {
        };
    }
    
    render() {
        return (
            <Provider store={store}>
                <Discovergift/>
            </Provider>
        )
    }
    

    }
    这里是入口文件
    const mapStateToProps = (state)=>
    ({
    Lang: state.Lang.selectedLang,
    Points: state.Points.newPoints
    })

    export default connect (mapStateToProps)(discovergift);
    路由文件把全局state注入
    import * as types from './ActionTypes';

    export function langChange(selectedLang){
    console.log('调用action: langChange');
    return (dispatch)=> {
    dispatch({
    'type' : types.LANG_CHANGE,
    selectedLang
    })
    }
    }
    action很简单,只存入一个值
    import * as types from '../actions/ActionTypes';

    const langInitState = {
    selectedLang: 'Portuguese'
    }

    export default function lang(state = langInitState, action = {}) {

    state = state || {
    		selectedLang: 'Portuguese'
    	}
    
    switch (action.type) {
    	case types.LANG_CHANGE:
    		console.log(" refresh lang ");
    		return Object.assign(
    			{}, state, {
    				selectedLang: action.selectedLang
    			}
    		)
    		break;
    	default:
    		return langInitState;
    }
    

    }
    reducer 改变state
    this.props.dispatch(langChange(“English”));
    组件内触发action的方法
    单独触发一个action的时候,该action对应的state改变,
    但是当触发第二个action的时候,对应的state改变,第一个action改变后的state却初始化了,就是我已经改变的Lang:‘Eglish’,变成了初始的Lang: 'Portuguese'
    import * as types from './ActionTypes';

    export function pointsChange(newPoints){
    console.log('action调用: pointsChange');
    return (dispatch)=> {
    dispatch({
    'type' : types.REFRESH_POINTS,
    newPoints
    })
    }
    }
    这是第二个action
    import * as types from '../actions/ActionTypes';

    const pointsInitState = {
    newPoints : 0
    };

    export default function points (state, action = {}){

    state = state || {
    		newPoints : 0
    	}
    
    switch (action.type) {
        case types.REFRESH_POINTS:
        	console.log(" refresh points ");
            return Object.assign(
                {} , state , {
                    newPoints :  parseInt(action.newPoints)
                }
            )
            break;
        default:
            return pointsInitState;
    }
    

    }
    第二个reducer。。。触发方法是一样的,求各位大神指点迷津,需要看什么代码我会再贴出来。跪求跪求


Log in to reply