如何在Redux Reducer中获得状态(如何组织Redux)?

I learn react with redux and can't understand how can I use state properties inside the reducers I have created reducers file inside /reducers folder

reducersText.js

export const textReducer = (state = [], action) => {
 switch (action.type) {
     case 'ADD_TEXT':
         return state = action.payload;
     default:
         return state;
 }
}
export const addCustomMinusWordReducer = (state = [], action) => {
 switch (action.type) {
     case 'ADD_CUSTOM_MINUS_WORD':
         return state = action.payload;
     default:
         return state;
 }
}

and I create index.js inside /reducers folder

import {textReducer, addCustomMinusWordReducer} from "./text"
import {combineReducers} from "redux"

const allReducers = combineReducers({
    text: textReducer,
    customMinusWords: addCustomMinusWordReducer
})

export default allReducers

My problem is I can not access the text state which created in textReducer inside addCustomMinusWordReducer. I found related link, but this is not what I am looking for. How can I use state in reducers? Thanks!

评论
  • 禁锢的誓言
    禁锢的誓言 回复

    Return only action.payload, state value should not be mutated directly inside reducer.

    export const textReducer = (state = [], action) => {
     switch (action.type) {
         case 'ADD_TEXT':
             return action.payload;
         default:
             return state;
     }
    }
    export const addCustomMinusWordReducer = (state = [], action) => {
     switch (action.type) {
         case 'ADD_CUSTOM_MINUS_WORD':
             return action.payload;
         default:
             return state;
     }
    }