-
Notifications
You must be signed in to change notification settings - Fork 96
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
Add reducer equivalent of the API #3
Comments
Hmm... Interesting. Have any ideas? Seems like a different package, but would share many of the same functionality. |
So it seems like it should be quite sufficient to add a parameter to It gets tricky, though, because That being said, there's a neat package react-enhanced-reducer-hook that augments A small preliminary sketch: const createPersistedReducer = (
key,
provider = global.localStorage,
reducer = (state, action) => typeof action === 'function' ? action(state) : action,
) => {
if (provider) {
const storage = createStorage(provider);
return initialState => usePersistedReducer(reducer, initialState, key, storage);
}
return useReducer;
}; with // Omitting code that stays the same
const usePersistedReducer = (reducer, initialState, key, { get, set }) => {
// ...same ...
const dispatch = action => {
const next = reducer(state, action);
setState(next);
return action;
};
// ... same ...
return [state, dispatch];
}; Alternatively, depending on how much you want to condense down the code changes, I can see all of this extra functionality being added in with ~5 lines of changed code by adding a conditional or two to the existing functions. |
Sorry it's taken been so long to respond. I've got a lot of pokers in the fire at once. This is a great plan. I'll try to get this implemented as soon as I can. I'm going something similar in a different project. |
This looks great, at first glance the main thing lacking is an equivalent of useReducer for more complex state management. Any interest in extending it a bit?
The text was updated successfully, but these errors were encountered: