Created
April 22, 2024 21:14
-
-
Save PaulieScanlon/64a5cc1950631b9b0bdc458ae1cf87d6 to your computer and use it in GitHub Desktop.
Example of server read-only component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/components/server-read-only-component.tsx | |
'use client'; | |
import { useAtom } from 'jotai'; | |
import { useHydrateAtoms } from 'jotai/utils'; | |
import { userAtom } from '../state'; | |
type Props = { | |
userData: { | |
isLoggedIn: boolean; | |
isAdmin: boolean; | |
}; | |
}; | |
export const ServerReadOnlyComponent: React.FunctionComponent<Props> = ({ userData }) => { | |
useHydrateAtoms([[userAtom, userData]]); | |
const [user] = useAtom(userAtom); | |
if (!user.isLoggedIn) return null; | |
return ( | |
<> | |
{user.isAdmin ? <div>An admin feature</div> : null} | |
<pre>{JSON.stringify(user, null, 2)}</pre> | |
</> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment