Docs CodeSandBox: Give explicit type to atom when used with useUpdateAtom
See original GitHub issueIn this CodeSandBox about useUpdateAtom, the setCount function has a TypeScript error, which arises due to TypeScript making a literal type from the 0
provided in the atom(0)
.
I am not sure if this type of inference for numbers is by design or a bug in TypeScript.
But having this error in a Docs Sandbox can make the first time onlooker feel like this isn’t ready, so I suggest the atom(0)
be converted to atom<number>(0)
The final code is:
import * as React from "react";
import { useRef } from "react";
import "./styles.css";
import { Provider, atom, useAtom } from "jotai";
import { useUpdateAtom } from "jotai/utils";
const countAtom = atom(0);
const Counter = () => {
const [count] = useAtom(countAtom);
return (
<div>
count: {count}
(rendered: {++useRef(0).current})
</div>
);
};
const Controls = () => {
const setCount = useUpdateAtom(countAtom);
const inc = () => setCount((c) => c + 1);
return (
<div>
<button onClick={inc}>+1</button>
(rendered: {++useRef(0).current})
</div>
);
};
export default function App() {
return (
<Provider>
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
<Counter />
<Controls />
</div>
</Provider>
);
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
utils - Atom not found · Issue #240 · pmndrs/jotai - GitHub
Currently, an atom must be used explicitly or dependent with other atoms. You can't update it before the initialization. We discuss about this ......
Read more >useUpdateAtom - Jotai
For newer versions of Jotai you can also use useSetAtom instead. import { atom, useAtom } from 'jotai'. import { useUpdateAtom } from...
Read more >async-fetch-to-set-primitive-atoms-via-a-write-only-atom-in-jotai
CodeSandbox is an online editor tailored for web applications. ... async-fetch-to-set-primitive-atoms-via-a-write-only-atom-in-jotai.
Read more >jotai-basic-sample-atoms-in-atom-splitAtom - CodeSandbox
CodeSandbox is an online editor tailored for web applications.
Read more >react-recoil-form examples - CodeSandbox
Learn how to use react-recoil-form by viewing and forking react-recoil-form example apps on CodeSandbox.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
So, it was sort of a doc issue. Thanks for reporting!
Yeah, updating Jotai in Sandbox fixed it, the ones in docs have a weird version like cs:[hash] or something, had to click on Update deps button to fix