Typescript errors when using useHydrateAtoms #2262
-
Hi. I'm trying to properly use useHydrateAtoms according to the docs. The code works fine in the browser, however I am facing some typescript issues. import { Provider, atom, useAtomValue } from "jotai";
import { useHydrateAtoms } from "jotai/utils";
import { type ReactNode } from "react";
const testAtom = atom("");
export default function JotaiTests() {
return (
<Provider>
<AtomsHydrator atomValues={[[testAtom, "hello"]]}> // <---- TS ERROR HERE
<MyComponent />
</AtomsHydrator>
</Provider>
);
}
//This component contains all the states and the logic
function MyComponent() {
const testAtomValue = useAtomValue(testAtom);
return <div>{testAtomValue}</div>;
}
function AtomsHydrator({
atomValues,
children,
}: {
atomValues: Parameters<typeof useHydrateAtoms>[0];
children: ReactNode;
}) {
useHydrateAtoms(atomValues);
return children;
} Which gives the following error : Digging further I've found that :
Am I using the function properly ? Thank you very much. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 9 replies
-
It used to work, but now useHydrateAtoms's type has a bit of hack, which breaks the example. This would be a naive fix: https://tsplay.dev/WyQ9bW import { WritableAtom, Provider, atom, useAtomValue } from "jotai";
import { useHydrateAtoms } from "jotai/utils";
import { type ReactNode } from "react";
const testAtom = atom("");
export default function JotaiTests() {
return (
<Provider>
<AtomsHydrator atomValues={[[testAtom, "hello"]]}>
<MyComponent />
</AtomsHydrator>
</Provider>
);
}
//This component contains all the states and the logic
function MyComponent() {
const testAtomValue = useAtomValue(testAtom);
return <div>{testAtomValue}</div>;
}
function AtomsHydrator({
atomValues,
children,
}: {
atomValues: Iterable<
readonly [WritableAtom<unknown, [any], unknown>, unknown]
>;
children: ReactNode;
}) {
useHydrateAtoms(atomValues as any);
return children;
} |
Beta Was this translation helpful? Give feedback.
-
I used this : https://jotai.org/docs/guides/initialize-atom-on-render and came up with the TS part myself. |
Beta Was this translation helpful? Give feedback.
-
I've tried your proposal. But we are trying to run our project with the rule : If we go this route, the following also works and seems less hacky to me : import { Provider, atom, useAtomValue } from "jotai";
import { useHydrateAtoms } from "jotai/utils";
import { type ReactNode } from "react";
const testAtom = atom("");
export default function JotaiTests() {
return (
<Provider>
<AtomsHydrator atomValues={[[testAtom, "hello"]]}>
<MyComponent />
</AtomsHydrator>
</Provider>
);
}
//This component contains all the states and the logic
function MyComponent() {
const testAtomValue = useAtomValue(testAtom);
return <div>{testAtomValue}</div>;
}
function AtomsHydrator({
atomValues,
children,
}: {
atomValues: unknown; // <--- TS hack #1
children: ReactNode;
}) {
useHydrateAtoms(atomValues as Parameters<typeof useHydrateAtoms>[0]); // <--- TS hack #2
return children;
} But I'm trying to avoid such hacks if possible. |
Beta Was this translation helpful? Give feedback.
-
I'll be honest, I had a look but I don't think I can understand or even try to fix the type definition on the useHydrateAtoms function. But the fact that Shall I open an issue ? A TS guru might help us find a good fix. 🙏 |
Beta Was this translation helpful? Give feedback.
I looked into this, but we don't have an ultimate solution, because TS can't extract types from overloaded function. Just considered about exporting an untyped util, but that's not nice. As a
Map
is untyped, my recommendation is the following:https://tsplay.dev/WvQ0Qm