在正式的tutorial之后,我真的不明白如何正确地使用react-redux@8.0.5
发送更新。
我的设置如下所示:
export const mySlice = createSlice({
name: 'MyItem',
initialState: { field1: 'hello, world', field2: 0 },
reducers: {
setField: (state: { field1: string; field2: number }, action: PayloadAction<string>) => {
state.field1 = action.payload;
},
},
});
export const store = configureStore({
reducer: {
myItem: mySlice.reducer,
},
});
export type AppDispatch = typeof store.dispatch;
在一个组件的内部,我挣扎着分派:
const dispatch: AppDispatch = useDispatch();
// Resembles the `dispatch(increment())}` from the offical example, but I get a type error and I can't pass my update data.
// Additionally, the signature looks wrong (it looks like a reducer)
dispatch(mySlice.actions.setField());
// Since the signature doesn't match, TypeScript complains with `Void function return value is used`
dispatch(mySlice.actions.setField('newValue'))
// TypeScript won't complain, but it doesn't seem like I could pass my update this way
dispatch(mySlice.actions.setField);
// TypeScript won't complain, but it's neither typed nor the recommended approach anyway
dispatch({ type: 'Idontexist', someValue: "hey" });
谢谢你的帮助!
1条答案
按热度按时间jljoyd4f1#
正确导出
现在您可以导入它并调度(setField())