jotaijs / jotai-scope

MIT License
60 stars 4 forks source link

Add default atom values #63

Open adamgen opened 3 weeks ago

adamgen commented 3 weeks ago

Currently adding a default values is highly complicated tho it's a very common use case.

The proposal is to add this api:

<ScopeProvider atoms={[atom1, atom2]} values={[value1, value]}>
adamgen commented 3 weeks ago

As a walk around I implemented this custom, hacky, provider

const ZeroAxisDefaultSetter: FC<PropsWithChildren<{ value?: boolean }>> = ({
  value,
}) => {
  const [, setZeroXaxis] = useAtom(zeroXaxisAtom);
  useEffect(() => {
    typeof value === "boolean" && setZeroXaxis(true);
  }, []);
  return "";
};
export const ZeroAxisProvider: FC<PropsWithChildren<{ value?: boolean }>> = ({
  children,
  value,
}) => {
  return (
    <ScopeProvider atoms={[zeroXaxisAtom]}>
      <ZeroAxisDefaultSetter value={value} />
      {children}
    </ScopeProvider>
  );
};