我怎样才能正确地将数据发送到 redux?
how can i send data to redux correctly?
我正在努力学习 Redux。我想做什么:用我发送给减速器的数据更新商店。到目前为止我做了什么:
let reduxData=[1,2,3];
const reduxState = useSelector((state)=>state)
const dispatch = useDispatch();
const {setReduxData} = bindActionCreators(actionCreators, dispatch);
我的操作:
export const setReduxData = (data) => {
return {
type: 'setReduxData',
payload: data
}
}
我的减速器:
export function reducer(state = {data: []}, action) {
switch (action.type) {
case 'setReduxData':
return {
...state,
data: [action.payload]
}
default:
return state
}
}
我对一般编程和 redux 仍然是新手,所以对于我确实犯下的任何业余错误,我深表歉意
let reduxData=[1,2,3];
const reduxState = useSelector((state)=>state)
const dispatch = useDispatch();
dispatch(setReduxData(reduxData)) // use it as you want e.g., on button click or any other action or in useEffect
使用以下代码更新您的操作代码:
export const setReduxData = (data) =>(dispatch) {
dispatch({
type: 'setReduxData',
payload: data
});
}
我正在努力学习 Redux。我想做什么:用我发送给减速器的数据更新商店。到目前为止我做了什么:
let reduxData=[1,2,3];
const reduxState = useSelector((state)=>state)
const dispatch = useDispatch();
const {setReduxData} = bindActionCreators(actionCreators, dispatch);
我的操作:
export const setReduxData = (data) => {
return {
type: 'setReduxData',
payload: data
}
}
我的减速器:
export function reducer(state = {data: []}, action) {
switch (action.type) {
case 'setReduxData':
return {
...state,
data: [action.payload]
}
default:
return state
}
}
我对一般编程和 redux 仍然是新手,所以对于我确实犯下的任何业余错误,我深表歉意
let reduxData=[1,2,3];
const reduxState = useSelector((state)=>state)
const dispatch = useDispatch();
dispatch(setReduxData(reduxData)) // use it as you want e.g., on button click or any other action or in useEffect
使用以下代码更新您的操作代码:
export const setReduxData = (data) =>(dispatch) {
dispatch({
type: 'setReduxData',
payload: data
});
}