0

I need to get the id of a user from the store to my action is there a way how to make it correct because this is the way how I using it but I think it's not correct is there a better way how to make this.

this is my action


export const editNote = (note, userId)=> {
  
    return async dispatch => {
      await axios.put(`http://localhost:8080/api/user/${userId}/note`, note
        )
            .then(response => {
                console.log("response from note api", response.body)
            })
            .catch(e => console.log(e))
    }
}

and this is how I called id

  const reducer = useSelector(state => state.user);

    const onSubmit = values => {
        console.log(values)
        dispatch(editNote(values, reducer.id))
    }
Lin Du
  • 88,126
  • 95
  • 281
  • 483
EagleCode
  • 125
  • 1
  • 9

1 Answers1

1

In actions you can pass getState parameter as same as you pass dispatch parameter to action. So your action like this (example):

export const editNote = (note, userId) => {
  return async (dispatch, getState) => {
    user = getState().user;
  }
}

with this code: user = getState().user you can access to the user and with getState() you can access to all your stored data in redux. (like state param in mapStateToProps function or useSelector hook in components)

Mohamadamin
  • 564
  • 5
  • 16