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.

[Question] Learning why derived setter is not causing suspend (add docs that only direct atom's suspend on write)

See original GitHub issue

Doing set from the dervied atom on an async child atom, is not causing the “Loading…” fallback of the <Suspend> to show.

  const [enabled, toggleEnabled] = useAtom(toggleEnabledAtom);
toggleEnable();

However doing it on the async child atom itself does show the fallback “Loading…”:

  const [enabled, setEnabled] = useAtom(enabledAtom);
setEnabled(true);

Below is playground. Can you please help me understand this?

Playground - https://codesandbox.io/s/jotai-playground-uuyd5?file=/src/App.js:0-1342

import { atom, Provider, useAtom } from "jotai";
import React, { Suspense } from "react";

const enabledAtom = atom("init", async (_get, set, nextValue) => {
  await new Promise((resolve) => setTimeout(resolve, 1000));
  set(enabledAtom, nextValue);
});
const toggleEnabledAtom = atom(
  (get) => get(enabledAtom),
  async (get, set) => {
    const enabled = get(enabledAtom);
    if (enabled) {
      await set(enabledAtom, false);
    } else {
      await set(enabledAtom, true);
    }
  }
);

function Atomed() {
  const [enabled, toggleEnabled] = useAtom(toggleEnabledAtom);
  return (
    <>
      <div>{String(enabled)}</div>
      <div>
        <button
          type="button"
          onClick={() => {
            toggleEnabled();
          }}
        >
          {enabled ? "Disable" : "Enable"}
        </button>
      </div>
    </>
  );
}
export default function App() {
  return (
    <Provider>
      <div className="App">
        <Suspense fallback="Loading...">
          <Atomed />
        </Suspense>
      </div>
    </Provider>
  );
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
Noitidartcommented, May 12, 2021

PR opened thanks to @angusryer’s notes - https://github.com/JLarky/jotai/pull/1

1reaction
dai-shicommented, Mar 5, 2021

@angusryer Thanks for the summary! Please do note this is about async write. In the readme, it’s called “Async actions”. We also have async read, which is “Derived async atoms” in the readme, and for this, all atoms having values (and their derived) will suspend.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jotai: The Ultimate React State Management - 100ms
Jotai is a relatively new state management library for React. It's simple, but make no mistakes, it's a robust library.
Read more >
Jotai vs. Recoil: What are the differences? - LogRocket Blog
Jotai and Recoil both have their benefits. This article compares the state management libraries to help you decide what's best for you.
Read more >
Chapter 5. The Rule Language - JBoss.org
This error indicates that the parser was looking for a particular symbol that it didn't find at the current input position. Here are...
Read more >
DevicePolicyManager - Android Developers
Any application calling an api may only pass as an argument a device administrator ... the user on how to resolve the noncompliance...
Read more >
C++ Core Guidelines - GitHub Pages
Following the rules will lead to code that is statically type safe, has no resource leaks, and catches many more programming logic errors ......
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