【问题标题】:Redux ged Id from store to actionRedux ged Id 从 store 到 action
【发布时间】:2021-07-22 08:12:20
【问题描述】:

我需要从商店获取用户的 id 到我的操作有没有办法让它正确,因为这是我使用它的方式,但我认为它不正确有没有更好的方法这个。

这是我的行动


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))
    }
}

这就是我所说的id

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

    const onSubmit = values => {
        console.log(values)
        dispatch(editNote(values, reducer.id))
    }

【问题讨论】:

标签: reactjs redux react-redux react-hooks


【解决方案1】:

在actions中你可以传递getState参数,就像你传递dispatch参数给action一样。 所以你的行为是这样的(例子):

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

使用此代码:user = getState().user 您可以访问用户,使用 getState() 您可以访问您在 redux 中存储的所有数据。 (如 mapStateToProps 函数中的状态参数或组件中的 useSelector 钩子)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-01
    • 2019-06-20
    • 1970-01-01
    • 2015-08-03
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多