Skip to content

Instantly share code, notes, and snippets.

View PaulieScanlon's full-sized avatar

Paul Scanlon PaulieScanlon

View GitHub Profile
@PaulieScanlon
PaulieScanlon / primitive-toggle-local-storage.tsx
Created April 22, 2024 21:18
Example local storage component
// src/components/primitive-toggle-local-storage.tsx
'use client';
import { useAtom } from 'jotai';
import { darkModeAtom } from '../state';
export const PrimitiveToggleLocalStorage = () => {
const [darkMode, setDarkMode] = useAtom(darkModeAtom);
@PaulieScanlon
PaulieScanlon / index.diff
Created April 22, 2024 21:17
Example of state
// src/state/index.ts
import { atom } from 'jotai';
+ import { atomWithStorage } from 'jotai/utils';
export const countAtom = atom(0);
export const objectAtom = atom({
foo: 'bar',
test: true,
@PaulieScanlon
PaulieScanlon / _layout.tsx
Created April 22, 2024 21:16
Example layout component
// src/layouts/_layout.tsx
import { Provider } from 'jotai';
type Props = {
children: React.ReactNode;
};
const RootLayout: React.FunctionComponent<Props> = async ({ children }) => {
return (
@PaulieScanlon
PaulieScanlon / server-read-only-component.tsx
Created April 22, 2024 21:14
Example of server read-only component
// 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: {
@PaulieScanlon
PaulieScanlon / default-page.tsx
Created April 22, 2024 21:12
Example of default page
// src/pages/default-page.tsx
import { ServerReadOnlyComponent } from '../components/server-read-only-component';
const Page = async () => {
const userData = await getServerData();
return (
<main>
<ServerReadOnlyComponent userData={userData} />
@PaulieScanlon
PaulieScanlon / index.diff
Created April 22, 2024 21:11
Example of state diff
// src/state/index.ts
import { atom } from 'jotai';
export const countAtom = atom(0);
export const objectAtom = atom({
foo: 'bar',
test: true,
});
@PaulieScanlon
PaulieScanlon / object-read-write-component.tsx
Created April 22, 2024 21:10
Example of read write component
// src/components/object-read-write-component.tsx
'use client';
import { useAtom } from 'jotai';
import { objectAtom } from '../state';
export const ObjectReadWriteComponent = () => {
const [obj, setObj] = useAtom(objectAtom);
@PaulieScanlon
PaulieScanlon / index.diff
Last active April 22, 2024 21:08
Example of state diff
// src/state/index.ts
import { atom } from 'jotai';
export const countAtom = atom(0);
+ export const objectAtom = atom({
+ foo: 'bar',
+ test: true,
+ });
@PaulieScanlon
PaulieScanlon / primitive-read-write-component.tsx
Created April 22, 2024 21:03
Example of read and write component
// src/components/primitive-read-write-component.tsx
'use client';
import { useAtom } from 'jotai';
import { countAtom } from '../state';
export const ReadWriteComponent = () => {
const [count, setCount] = useAtom(countAtom);
@PaulieScanlon
PaulieScanlon / primitive-read-only-component.tsx
Created April 22, 2024 21:01
Example of read only component
// src/components/primitive-read-only-component.tsx
'use client';
import { useAtom } from 'jotai';
import { countAtom } from '../state';
export const ReadOnlyComponent = () => {
const [count] = useAtom(countAtom);