Function merge

Merges the current state with a state modifier. Useful for creating custom set functions.

import { createStore, merge, StateModifier } from "@fransek/statekit";

const store = createStore(
{
counter: {
count: 0,
},
// ...
},
(set) => {
const setCounter = (counter: StateModifier<{ count: number }>) =>
set((state) => ({
counter: merge(state.counter, counter),
}));

return {
increment: () => setCounter((state) => ({ count: state.count + 1 })),
decrement: () => setCounter((state) => ({ count: state.count - 1 })),
reset: () => setCounter({ count: 0 }),
// ...
};
},
);
  • Type Parameters

    • T extends object

    Parameters

    • currentState: T
    • stateModifier: StateModifier<T>

      A function or object that modifies the state.

    Returns T

    The new state object, which is a combination of the current state and the state modifier.