flaze-dev / envy

A simple state management library
0 stars 0 forks source link

Memoized state management (concept) #11

Open ingoandelhofs opened 1 year ago

ingoandelhofs commented 1 year ago
const ctx = () => {
  const [valueSelector, setValue, getValue] = useMemoizedState();

  return {
    valueSelector,
    setValue,
  }
}

const [useValueContex, ValueContextProvider] = generateContext(ctx);
const context = useValueContext();
const value = useSelector(context.valueSelector);
const readValue = context.getValue();
ingoandelhofs commented 1 year ago

const dispatch = () => {

}

export const ctx = (contextValue: any) => {

  const updateSize = useCallback(
    (size: number) => contextValue.dispatch({type: "updateSize", data: {size}}),
    [contextValue],
  );

  const updateSpacing = useCallback(
    (spacing: number) => contextValue.dispatch({type: "updateSpacing", data: {spacing}}),
    [contextValue],
  );

  return {
    updateSize,
    updateSpacing,
  }
}

const {useContext, useContextSelector, ContextProvider} = generateMemoizedContext();