-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial commit * update to typescript * align package.json * fix naming * move to subscribe function * working, more concise solution * fix eslint errors * rename import according to suggestion * add dependencies to package.json and pnpm-lock * add skipLibCheck to tsconfig
- Loading branch information
Showing
8 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "jotai-location-example", | ||
"version": "0.1.0", | ||
"private": true, | ||
"dependencies": { | ||
"@types/react": "latest", | ||
"@types/react-dom": "latest", | ||
"history": "5.1.0", | ||
"jotai": "latest", | ||
"jotai-location": "latest", | ||
"react": "latest", | ||
"react-dom": "latest", | ||
"react-router-dom": "6.8.0", | ||
"react-scripts": "latest", | ||
"typescript": "latest" | ||
}, | ||
"scripts": { | ||
"start": "react-scripts start", | ||
"build": "react-scripts build", | ||
"test": "react-scripts test", | ||
"eject": "react-scripts eject" | ||
}, | ||
"browserslist": [">0.2%", "not dead", "not ie <= 11", "not op_mini all"] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<html> | ||
<head> | ||
<title>jotai-location example</title> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
</body> | ||
</html> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { | ||
unstable_HistoryRouter as HistoryRouter, | ||
Routes, | ||
Route, | ||
Link, | ||
} from 'react-router-dom'; | ||
import { atomWithLocation } from 'jotai-location'; | ||
import { useAtomValue } from 'jotai'; | ||
import * as React from 'react'; | ||
import history from './routerHistory'; | ||
|
||
const Route1 = <h1>Hello</h1>; | ||
const Route2 = <h1>World</h1>; | ||
const location = atomWithLocation({ | ||
getLocation: () => ({ | ||
searchParams: new URLSearchParams(history.location.search), | ||
...history.location, | ||
}), | ||
subscribe: (callback) => history.listen(callback), | ||
}); | ||
|
||
const App = () => { | ||
const loc = useAtomValue(location); | ||
return ( | ||
<div | ||
className="App" | ||
style={{ | ||
textAlign: 'center', | ||
display: 'flex', | ||
flexDirection: 'column', | ||
gap: '16px', | ||
}} | ||
> | ||
{/* @ts-expect-error: https://github.com/remix-run/react-router/issues/9630#issuecomment-1341643731 */} | ||
<HistoryRouter history={history}> | ||
current pathname in atomWithLocation: "{loc.pathname}" | ||
<div style={{ display: 'flex', gap: '16px', placeContent: 'center' }}> | ||
<Link to="/1"> to 1</Link> | ||
<Link to="/1/123"> to 1/123</Link> | ||
<Link to="/2"> to 2</Link> | ||
<Link to="/2/123"> to 2/123</Link> | ||
</div> | ||
<Routes> | ||
<Route path="/1" element={Route1} /> | ||
<Route path="/2" element={Route2} /> | ||
</Routes> | ||
</HistoryRouter> | ||
</div> | ||
); | ||
}; | ||
export default App; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import * as React from 'react'; | ||
import { createRoot } from 'react-dom/client'; | ||
|
||
import App from './App'; | ||
|
||
const ele = document.getElementById('app'); | ||
if (ele) { | ||
createRoot(ele).render(<App />); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { createBrowserHistory } from 'history'; | ||
|
||
const routerHistory = createBrowserHistory(); | ||
export default routerHistory; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
"baseUrl": ".", | ||
"paths": { | ||
"jotai-location": ["./src"] | ||
} | ||
}, | ||
"skipLibCheck": true | ||
} | ||
} |