redux-toolkit 操作未在 reducer 中触发
redux-toolkit action not triggered in reducer
我正在尝试使用@reduxjs/Toolkit 触发一个简单的操作,但它不起作用。
我看到动作已被分派,但好像 slice reducer 没有在听它什么的。
const say = createAction("ui/say", what => ({ payload: what }));
const uiSlice = createSlice({
name: "ui",
initialState: { said: "" },
reducers: {
[say.type]: (state, action) => {
console.log("saying", action.payload); //<-- not showing, why?
state.currentList = action.payload;
}
}
});
const store = configureStore({
reducer: combineReducers({
ui: uiSlice.reducer
})
});
const Chat = () => {
const dispatch = useDispatch();
const [whatToSay, setWhatToSay] = useState("");
const whatWasSaid = useSelector(state => state.ui.said);
const onSubmit = e => {
e.preventDefault();
dispatch(say(whatToSay));
setWhatToSay("");
};
return (
<div>
<form onSubmit={onSubmit}>
<input type="text" onChange={e => setWhatToSay(e.target.value)} />
<button>Say</button>
</form>
{whatWasSaid ? <p>You said: {whatWasSaid}</p> : <p>Say something</p>}
</div>
);
};
这是一个最小的复制示例:
https://codesandbox.io/s/redux-toolkit-0tzxs?file=/src/index.js
我认为你不匹配createSlice
API。
根据您的代码,您试图为某个操作实现侦听器,因此您可能希望改用 extraReducers
:
const uiSlice = createSlice({
name: "ui",
initialState: { said: "" },
// Not reducers: {}
extraReducers: {
[say.type]: (state, action) => {
console.log("saying", action.payload);
state.currentList = action.payload;
}
}
});
注意 createSlice
的 reducers
属性 API:
reducers: Object<string, ReducerFunction | ReducerAndPrepareObject>
如果你想在reducers
中使用say
,应该是:
const say = (state, payload) => {
console.log("saying", payload);
state.currentList = payload;
};
const uiSlice = createSlice({
name: "ui",
initialState: { said: "" },
reducers: { say }
});
// Usage
dispatch(uiSlice.actions.say(whatToSay));
@markerikson: with createSlice
, the reducers field is for defining reducers and generating actions that will match those reducers. The extraReducers
field is for handling actions that were already defined elsewhere.
我正在尝试使用@reduxjs/Toolkit 触发一个简单的操作,但它不起作用。
我看到动作已被分派,但好像 slice reducer 没有在听它什么的。
const say = createAction("ui/say", what => ({ payload: what }));
const uiSlice = createSlice({
name: "ui",
initialState: { said: "" },
reducers: {
[say.type]: (state, action) => {
console.log("saying", action.payload); //<-- not showing, why?
state.currentList = action.payload;
}
}
});
const store = configureStore({
reducer: combineReducers({
ui: uiSlice.reducer
})
});
const Chat = () => {
const dispatch = useDispatch();
const [whatToSay, setWhatToSay] = useState("");
const whatWasSaid = useSelector(state => state.ui.said);
const onSubmit = e => {
e.preventDefault();
dispatch(say(whatToSay));
setWhatToSay("");
};
return (
<div>
<form onSubmit={onSubmit}>
<input type="text" onChange={e => setWhatToSay(e.target.value)} />
<button>Say</button>
</form>
{whatWasSaid ? <p>You said: {whatWasSaid}</p> : <p>Say something</p>}
</div>
);
};
这是一个最小的复制示例: https://codesandbox.io/s/redux-toolkit-0tzxs?file=/src/index.js
我认为你不匹配createSlice
API。
根据您的代码,您试图为某个操作实现侦听器,因此您可能希望改用 extraReducers
:
const uiSlice = createSlice({
name: "ui",
initialState: { said: "" },
// Not reducers: {}
extraReducers: {
[say.type]: (state, action) => {
console.log("saying", action.payload);
state.currentList = action.payload;
}
}
});
注意 createSlice
的 reducers
属性 API:
reducers: Object<string, ReducerFunction | ReducerAndPrepareObject>
如果你想在reducers
中使用say
,应该是:
const say = (state, payload) => {
console.log("saying", payload);
state.currentList = payload;
};
const uiSlice = createSlice({
name: "ui",
initialState: { said: "" },
reducers: { say }
});
// Usage
dispatch(uiSlice.actions.say(whatToSay));
@markerikson: with
createSlice
, the reducers field is for defining reducers and generating actions that will match those reducers. TheextraReducers
field is for handling actions that were already defined elsewhere.