Skip to content

Commit

Permalink
feat: add jotai example
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermedeandrade committed Oct 17, 2022
1 parent 9d347a9 commit 41b4e33
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@tanstack/react-location": "^3.7.4",
"jotai": "^1.8.5",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"recoil": "^0.7.6"
Expand Down
39 changes: 39 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {Outlet, ReactLocation, Router} from '@tanstack/react-location'

import {Home} from './home'
import {RecoilCharacterCounter} from './examples/recoil'
import {JotaiCharacterCounter, RecoilCharacterCounter} from './examples'

const location = new ReactLocation()

const routes = [
{path: '/', element: <Home />},
{path: 'recoil', element: <RecoilCharacterCounter />},
{path: 'jotai', element: <JotaiCharacterCounter />},
]

function App() {
Expand Down
2 changes: 2 additions & 0 deletions src/examples/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './jotai'
export * from './recoil'
41 changes: 41 additions & 0 deletions src/examples/jotai.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type {ChangeEvent} from 'react'
import {atom, useAtom} from 'jotai'

const textAtom = atom('')

const charCountAtom = atom(get => {
const text = get(textAtom)
return text.length
})

function TextInput() {
const [text, setText] = useAtom(textAtom)

const onChange = (event: ChangeEvent<HTMLInputElement>) => {
setText(event.target.value)
}
return (
<div>
<input type="text" value={text} onChange={onChange} />
<br />
Echo: {text}
</div>
)
}

function CharacterCount() {
const count = useAtom(charCountAtom)
return <>Character Count: {count}</>
}

function JotaiCharacterCounter() {
return (
<>
<h1>Jotai</h1>
<TextInput />
<CharacterCount />
</>
)
}

export {JotaiCharacterCounter}
1 change: 1 addition & 0 deletions src/examples/recoil.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function CharacterCount() {
function RecoilCharacterCounter() {
return (
<RecoilRoot>
<h1>Recoil</h1>
<TextInput />
<CharacterCount />
</RecoilRoot>
Expand Down

0 comments on commit 41b4e33

Please sign in to comment.