Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage with immutable libraries (mutative) #135

Open
IrvingArmenta opened this issue Sep 2, 2024 · 1 comment
Open

Usage with immutable libraries (mutative) #135

IrvingArmenta opened this issue Sep 2, 2024 · 1 comment

Comments

@IrvingArmenta
Copy link

I would like to use mutative -> https://github.com/mutativejs/use-mutative useMutativeReducer to use and update the reducer for the context

const Provider = ({ children }: { children: ReactNode }) => {
  const [state, dispatch] = useMutativeReducer(reducer, initialState);

  return (
    <context.Provider value={[state, dispatch]}>{children}</context.Provider>
  );
};

The reducer looks something like this:

export const reducer = (draft: State, action: Actions) => {
  switch (action.type) {
    case 'increment':
      draft.count++;
      break;
    case 'decrement':
      draft.count--;
      break;
    case 'setText':
      draft.text = action.payload.text;
      break;
    case 'addTodo': {
      const newLength = draft.todos.length++;
      draft.todos[newLength] = action.payload;
      break;
    }
    case 'setError': {
      draft.error = action.payload;
      break;
    }
    case 'setLoading':
      draft.loading = action.payload;
      break;
    default:
      throw new Error(`unknown action type: ${(action as Actions).type}`);
  }
};

Question

Is there any performance or possible breaking implementation issue by using something like this?

Immer would be another example of this kind of library.

@dai-shi
Copy link
Owner

dai-shi commented Sep 2, 2024

I don't think there're any performance implications. There's also use-immer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants