immerjs / use-immer

Use immer to drive state with a React hooks
MIT License
4.04k stars 92 forks source link

[FEATURE] Passing a function for draft or value for direct state usage #76

Closed blackshot closed 3 years ago

blackshot commented 3 years ago

This PR enables the useImmer hook to be used as a normal useState passing a value as argument with still allowing the user to use the immer feature.

const [state, setState] = useImmer([]);

// normal setState usage
    setState(["this", "is", "the", "new", "state"]);
// or immer usage
    setState( draft => {
        draft.push("my new item")
    });
mweststrate commented 3 years ago

Thanks for the PR! LGTM, but would you mind updating the documentation as well? Thanks!

mweststrate commented 3 years ago

Published as 0.5.0