如何在Redux工具包中使用getState()

时间:2020-10-06 10:47:55

标签: reactjs redux react-redux redux-toolkit

我有以下内容和功能。如何获取存储在initialState中的值?

const example = createSlice({
  name: "example",
  initialState: {
   firstName: hello,
   lastName: world
  },
  reducers: {}
 })

export const create = (id) => {
  return async (dispatch) => {
      const currentState = getState()
      console.log(currentState) 
  };
};

我是否必须使用useSelector并将值再次传递给该函数?理想情况是希望直接从已保存的状态中获取数据

1 个答案:

答案 0 :(得分:1)

export const create = (id) => {
  return async (dispatch, getState) => {
       const currentState= getState().example;
      console.log(currentState) 
  };
};

您可以使用上面的代码访问切片状态。