JonJon
JonJon

Reputation: 179

How to getState() in Redux Toolkit

I have the following slice and function. How can I get values stored in my 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) 
  };
};

Do I have to use useSelector and pass values down to the function again? Ideally would like to get data directly from the already saved state

Upvotes: 9

Views: 32491

Answers (1)

Freddy.
Freddy.

Reputation: 1745

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

You can access the slice state using the above code.

Upvotes: 6

Related Questions