Home > Blockchain >  how can i send data to redux correctly?
how can i send data to redux correctly?

Time:03-02

I am trying to learn Redux. what I want to do: update the store with the data I send to the reducer. what I've done so far:

let reduxData=[1,2,3];
const reduxState = useSelector((state)=>state)
const dispatch = useDispatch();
const {setReduxData} = bindActionCreators(actionCreators, dispatch);

my action:

export const setReduxData = (data) => {
return {
    type: 'setReduxData',
    payload: data
 }
}

my reducer:

export function reducer(state = {data: []}, action) {
  switch (action.type) {
    case 'setReduxData':
      return {
        ...state,
        data: [action.payload]
      }
      default:
        return state
  }
}

I am still new to both programming in general and redux so I apologize for any amateur mistakes I surely have made

CodePudding user response:

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

Update your action code with the following code:

export const setReduxData = (data) =>(dispatch) {
dispatch({
    type: 'setReduxData',
    payload: data
 });
}
  • Related