Replies: 4 comments 7 replies
-
I don't think so.
Yeah, I agree. I can help reviewing if someone creates a small one. |
Beta Was this translation helpful? Give feedback.
-
I successfully used a nested approach in this sample repo to share this across the entire app via 'use client';
import { Provider } from 'jotai';
import { useHydrateAtoms } from 'jotai/utils';
import React from 'react';
import { helloAtom } from '@/store';
type Props = {
initialState: string | null;
children: React.ReactNode;
};
function StateWrapper({ initialState, children }: Props) {
useHydrateAtoms([[helloAtom, initialState]]);
return children;
}
export default function JotaiProvider({ initialState, children }: Props) {
return (
<Provider>
<StateWrapper initialState={initialState}>{children}</StateWrapper>
</Provider>
);
} I needed a second wrapper, because it wouldn't work if I called 'use client';
import { Provider } from 'jotai';
import { useHydrateAtoms } from 'jotai/utils';
import React from 'react';
import { helloAtom } from '@/store';
type Props = {
initialState: string | null;
children: React.ReactNode;
};
export default function JotaiProvider({ initialState, children }: Props) {
useHydrateAtoms([[helloAtom, initialState]]);
return (
<Provider>{children}</Provider>
);
} Of course, in this example, However, if you wanted to keep |
Beta Was this translation helpful? Give feedback.
-
Can I get an example on how to do this with
"use server";
import { apiKeysAtom } from "@/atoms/api-keys.atom";
import ApiKeyActionBar from "@/components/api-keys/ApiKeyActionBar";
import ApiKeyHeaderContent from "@/components/api-keys/ApiKeyHeaderContent";
import ApiKeysTable from "@/components/api-keys/ApiKeysTable";
import PageFrame from "@/components/common/page/PageFrame";
import PageHeader from "@/components/common/page/PageHeader";
import TablePagination from "@/components/common/table/TablePagination";
import { ORG_ID_COOKIE_NAME } from "@/config/constants.common";
import { ApiKeysApi } from "@/lib/api/ApiKeysApi";
import { useHydrateAtoms } from "jotai/utils";
import { cookies } from "next/headers";
export default async function ApiKeysPage() {
const organizationId = cookies().get(ORG_ID_COOKIE_NAME) as unknown as string;
const result = await ApiKeysApi.getApiKeys(organizationId);
useHydrateAtoms([[apiKeysAtom, result?.apiKeys ?? []]]);
return (
<PageFrame>
<PageHeader title="API Keys" subtitle="" rightColumn={<ApiKeyHeaderContent />} />
<ApiKeyActionBar />
<ApiKeysTable />
<TablePagination />
</PageFrame>
);
}
|
Beta Was this translation helpful? Give feedback.
-
I also falled into this "useHydrateAtoms is not a function" error thinking this hydration method is a server side magic. I think it would be clearer to add |
Beta Was this translation helpful? Give feedback.
-
I've seen the page on SSR, but I'm still not sure how to implement seeding a jotai atom on the server and hydrating it back on the client side via
useHydrateAtoms
.There is a CodeSandbox embedded on that page, but it says the account is out of credits and trying to fork it says permission denied. The Vercel example doesn't use SSR and all the other examples I've found use the pages model.
I've also seen this thread and it's a bit confusing with all sorts of options.
This Stack Overflow thread seems to cover the gist of passing it from a server component to a client component via props, and their client component seems to call
useHydrateAtom
beforeuseAtom
, which makes sense, but if the client re-renders, this runs again:From other discussions, this sounds fine, but are there performance implications of this? Should I attempt to avoid this?
In short: seeing a short example repo would really help news users of this library like me.
Beta Was this translation helpful? Give feedback.
All reactions