question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Docs CodeSandBox: Give explicit type to atom when used with useUpdateAtom

See original GitHub issue

In 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). image

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:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
dai-shicommented, Jul 6, 2021

So, it was sort of a doc issue. Thanks for reporting!

0reactions
PuruVJcommented, Jul 6, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found