反应 redux 导入减速器调度
react redux importing reducer dispatch
我正在尝试遵循 react-redux 模板中的反例,但我似乎无法导出自己的 setOwner
reducer。
ownerSelector.tsx
:
export const ownerSlice = createSlice({
name: 'owner',
initialState,
reducers:{
setOwner: (state) => { //Export this reducer
let own = document.getElementById("OwnerSelector") as HTMLSelectElement
state.id = parseInt(own!!.options[own.selectedIndex].value);
}
},
extraReducers: (builder) => {}
})
export const selectOwner= (state: RootState) => state.selectedOwner.id;
export default ownerSlice.reducer;
我想在 carForm.tsx
中导入 setOwner
的位置:
import {setOwner} from './ownerSelector'; // error here
let submitHandler = (e: React.FormEvent<HTMLElement>) => {
e.preventDefault();
dispatch(setOwner())
const formData = {
car: {
year: year,
make: make,
model: model,
price: price,
person_id: owner,
}
}
dispatch(createCarAsync(formData));
resetState();
}
导入时的错误是:Module '"./ownerSelector"' has no exported member 'setOwner'. Did you mean to use 'import setOwner from "./ownerSelector"' instead?ts(2614)
我做错了什么?谢谢。
在 redux 工具包和任何你需要调度操作的 redux 中。
export const { setOwner } = ownerSlice .actions
//now you can use it in dispatch
我正在尝试遵循 react-redux 模板中的反例,但我似乎无法导出自己的 setOwner
reducer。
ownerSelector.tsx
:
export const ownerSlice = createSlice({
name: 'owner',
initialState,
reducers:{
setOwner: (state) => { //Export this reducer
let own = document.getElementById("OwnerSelector") as HTMLSelectElement
state.id = parseInt(own!!.options[own.selectedIndex].value);
}
},
extraReducers: (builder) => {}
})
export const selectOwner= (state: RootState) => state.selectedOwner.id;
export default ownerSlice.reducer;
我想在 carForm.tsx
中导入 setOwner
的位置:
import {setOwner} from './ownerSelector'; // error here
let submitHandler = (e: React.FormEvent<HTMLElement>) => {
e.preventDefault();
dispatch(setOwner())
const formData = {
car: {
year: year,
make: make,
model: model,
price: price,
person_id: owner,
}
}
dispatch(createCarAsync(formData));
resetState();
}
导入时的错误是:Module '"./ownerSelector"' has no exported member 'setOwner'. Did you mean to use 'import setOwner from "./ownerSelector"' instead?ts(2614)
我做错了什么?谢谢。
在 redux 工具包和任何你需要调度操作的 redux 中。
export const { setOwner } = ownerSlice .actions
//now you can use it in dispatch