[Question] Learning why derived setter is not causing suspend (add docs that only direct atom's suspend on write)
See original GitHub issueDoing 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:
- Created 3 years ago
- Comments:19 (8 by maintainers)
Top 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 >
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
PR opened thanks to @angusryer’s notes - https://github.com/JLarky/jotai/pull/1
@angusryer Thanks for the summary! Please do note this is about async
write
. In the readme, it’s called “Async actions”. We also have asyncread
, which is “Derived async atoms” in the readme, and for this, all atoms having values (and their derived) will suspend.