Skip to content

How to create local state? #2112

Answered by EskiMojo14
razb-viola asked this question in Q&A
Discussion options

You must be logged in to vote

Yeah, Redux is by nature a global state solution.

It's worth noting though that if you're a particular fan of the slice pattern, it's possible to use reducers and action creators made with createSlice in combination with useReducer.

const counterSlice = createSlice({
  name: "counter",
  initialState: { value: 0 },
  reducers: {
    increment: (state) => {
      state.value++;
    },
  },
});

const { increment } = counterSlice.actions;

function Component() {
  const [counterState, counterDispatch] = useReducer(
    counterSlice.reducer,
    counterSlice.getInitialState(),
  );
  return (
    <>
      <p>Value is {counterState.value}</p>
      <button onClick={() => counterDispatch(incre…

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
5 replies
@EskiMojo14
Comment options

Answer selected by razb-viola
@razb-viola
Comment options

@phryneas
Comment options

@EskiMojo14
Comment options

@razb-viola
Comment options

Comment options

You must be logged in to vote
1 reply
@EskiMojo14
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants