【问题标题】:Modify one state property in two Reducers in Redux在 Redux 中修改两个 Reducer 中的一个 state 属性
【发布时间】:2020-05-31 11:42:17
【问题描述】:

我在我的状态下对 Redux 有一个小问题,我需要能够在两个 Reducer 中修改我的计数器属性,因为它对两个 reducer 的值更新(增量,减量)抱歉,我是 Redux 的新手,并尝试理解这个概念。

计数器组件


        return(
            <>
                <div className="flex justify-center">
                    <div className='font-bold text-4xl text-blue-600 bg-gray-300 p-6 m-3 rounded-full'>The Result :
                        {this.props.counter}
                    </div>
                </div>
                <section className='flex p-5 m-3 justify-center'>
                    <button onClick={this.props.increment} className={`bg-green-400 ${btnDefault}`} >Increment</button>
                    <button onClick={() => this.props.decrement(this.props.counter)} className={`bg-red-400 ${btnDefault}`}>Decrement</button>
                </section>
            </>
        )
    }
}


const mapStateToProps = state => {
    return {
        counter: state.incReducer.counter,
    }
};
const mapDispatchToProps = dispatch => {
    return {
        increment: () => dispatch(counterAction('INCREMENT')),
        decrement: (counter) => dispatch(counterAction('DECREMENT',counter))
    }
};

export default connect(mapStateToProps, mapDispatchToProps)(Counter)

增量减速器

const initialState = {
    counter: 0,
};

const reducer = (state = initialState, action) => action.type === actionType.INCREMENT ?
        {
        ...state,
        counter: state.counter + 1
        }
    : state;


export default reducer;

递减器

const initState = {

};

const reducer = (state = initState, action) => action.type === actionType.DECREMENT ?
    {
        ...state,
        counter: action.counter - 1
    }
    : state;

export default reducer;

Index.js

const reducer = combineReducers({
    incReducer,
    decReducer
});

const store = createStore(reducer);

ReactDOM.render(<Provider store={store}><App/></Provider> , document.getElementById('root'));

【问题讨论】:

  • 您不应该在多个 reducer 中复制相同的值。一个 store 值只能在一个地方,即在一个 reducer 中定义和修改。

标签: reactjs redux react-redux


【解决方案1】:

你只是从错误的角度接近它,你不应该在两个 reducer 中修改相同的变量,而是希望有一个 counterReducer 来处理修改计数器的所有操作:

const initialState = {
    counter: 0,
};

const reducer = (state = initialState, action) => {
    switch(action.type) {
        case actionType.INCREMENT:
            return {
                ...state,
                counter: state.counter + 1
            };
            break;
        case actionType.DECREMENT:
            return {
                ...state,
                counter: state.counter - 1
            }
            break;
        default:
            return state;
    }
}

export default reducer;

拆分成多个reducer 是保持代码井井有条的一种方式,但是您永远不会有两个reducer 可以各自修改相同的东西。每个 reducer 都应该处理整体状态的某个“部分”,例如

const totalState = {
    activeUser: { /* username, email, phone, etc */ },
    friends: [],
    subscriptions: [],
    history: [],
    // etc.
}

然后你可能有一个activeUserReducerfriendsReducersubscriptonsReducerhistoryReducer 等。每个人都会处理这部分状态,但永远不会进入另一个减速器状态。

如果您需要通过一个操作修改两个不同的属性,您只需这样做:

const mapDispatchToProps = dispatch => {
    return {
        increment: () => {
            dispatch(counterAction('INCREMENT'));
            dispatch(someOtherAction());
        }
    };
};

【讨论】:

  • 我知道我可以做到这一点,但我认为在更大的应用程序中可能会发生同样的情况,我必须修改相同的属性,所以我将它分配给两个减速器以掌握如何在示例中做到这一点方式。
  • 您永远不必在两个不同的 reducer 中修改 相同的属性 - 一个 reducer 必须“拥有”该数据。
猜你喜欢
  • 1970-01-01
  • 2016-06-10
  • 2016-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-05
  • 2017-10-02
  • 2021-01-05
相关资源
最近更新 更多