Last active
October 15, 2020 07:36
-
-
Save phobon/369f63d38db853dba8801500ed0a19ea to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// App.js | |
import { Provider } from "jotai"; | |
import { Layout } from "./Layout"; | |
const App = ({ children }) => { | |
return ( | |
<Provider> | |
<Layout> | |
{children} | |
</Layout> | |
</Provider> | |
); | |
}; | |
export default App; |
This file contains hidden or 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
// atoms.js | |
import { atom } from "jotai"; | |
export const themeAtom = atom({ theme: "dark", fontSize: "big" }); |
This file contains hidden or 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
export const Form = ({ ...props }) => { | |
const name = useState(null); | |
const address = useState(null); | |
const other = useState(null); | |
const fields = [name, address, other]; | |
return ( | |
<form> | |
{fields.map(([field, setField], index) => ( | |
<input type="text" onChange={e => setField(e.value)} value={field} /> | |
)} | |
</form> | |
); | |
}; |
This file contains hidden or 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
// Layout.js | |
import { useAtom } from "jotai"; | |
import { themeAtom } from "./atoms"; | |
export const Layout = ({ children, ...props }) => { | |
const [{ theme, fontSize }, setTheme] = useAtom(themeAtom); | |
return ( | |
<main className={`myapp__theme--${theme}`}> | |
<header className={`myapp__theme__fontsize--${fontSize}`}> | |
<h1>My app!</h1> | |
<button onClick={() => setTheme(t => ({ ...t, theme: t.theme === "dark" ? "light" : "dark" }))}>Toggle theme</button> | |
</header> | |
{children} | |
</main> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment