-
Hi @dai-shi , I hope I properly understood your request in #1900 and moved my question to this new Issue. The problem i'm facing: I want to make a reusable Here is the code: import { FunctionComponent, PropsWithChildren } from "react"
import { Provider, WritableAtom } from "jotai"
import { useHydrateAtoms } from "jotai/utils"
export type AnyWritableAtom = WritableAtom<unknown, never[], unknown>
interface InitialProps extends PropsWithChildren {
initialValues: Array<readonly [AnyWritableAtom, unknown]>
}
const HydrateAtoms: FunctionComponent<InitialProps> = ({
children,
initialValues,
}) => {
// Typecasting because i can't seem to make a generic type out of this one.
useHydrateAtoms(initialValues as Array<readonly [AnyWritableAtom, never]>)
return <>{children}</>
}
const JotaiTestProvider: FunctionComponent<InitialProps> = ({
children,
initialValues,
}) => {
return (
<Provider>
<HydrateAtoms initialValues={initialValues}>{children}</HydrateAtoms>
</Provider>
)
}
const createAtomWrapper = (initialValues: InitialAtomValues) => {
const AtomWrapper = ({ children }: PropsWithChildren) => (
<JotaiTestProvider initialValues={initialValues}>
{children}
</JotaiTestProvider>
)
return AtomWrapper
} My initial values are tuples of writable atoms - primitive atoms and atoms with storage. In the example below, the const { result } = renderHook(
() => {
return useCallDuration()
},
{
wrapper: createAtomWrapper([
[agentAtom, agent] as const,
[callDurationDataAtom, null] as const,
[currentContactIdAtom, taskId] as const,
]),
},
) I would like to ask if there is a better way of doing this, if there is something i'm missing. Thank you in advance for any lead on better understanding this. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
And immediately after opening this discussion i've noticed this one where the Iterable is used 🤦. Is the only solution to make a map out of the array of tuples?
Sorry for not noticing the active Discussion before :( |
Beta Was this translation helpful? Give feedback.
-
I actually mean to open a PR to see your suggestion, but if you need more discussion, this is much better than #1900. |
Beta Was this translation helpful? Give feedback.
I see. Then,
Map
might be a good solution.How about this?
Repro: https://tsplay.dev/WyLXAm
A possible fix: https://tsplay.dev/W4gxvm