To parallel the signature of a useState hook, it would make sense to add useStateProperty which composes a value and a setter. This would guide people away from using partitions which might otherwise seem like a worthwhile approach (but has complexity and overhead when you didn't actually want to compose a whole new long-lived store). See https://github.com/cefn/watchable/issues/35
A draft implementation would be this...
export function useStateProperty<
S extends Record<PropertyKey, unknown>,
K extends keyof S
>(store: Store<S>, key: K) {
const setter = useCallback(
(value: S[K]) => {
store.write({
...store.read(),
[key]: value,
});
},
[store, key]
);
const selector = useCallback((state: Immutable<S>) => state[key], [key]);
const value = useSelected(store, selector);
return [value, setter] as const;
}
To parallel the signature of a
useState
hook, it would make sense to adduseStateProperty
which composes a value and a setter. This would guide people away from using partitions which might otherwise seem like a worthwhile approach (but has complexity and overhead when you didn't actually want to compose a whole new long-lived store). See https://github.com/cefn/watchable/issues/35A draft implementation would be this...