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] use with concurrent mode

See original GitHub issue

I try to use jotai as a concurrent-mode library but I failed to produce the result I want as I’m following https://reactjs.org/docs/concurrent-mode-patterns.html#splitting-high-and-low-priority-state

Expected:

When I type in the input, the result is updated with staled data of searchResultAtom because I’m updating it inside a startTransition.

Actual

The App goes into the full-suspended state and displays the fallback. isPending never becomes true.

How can I use jotai with concurrent mode correctly?

import React, { useState, unstable_useTransition } from 'react';
import { useAtom, atom } from 'jotai';

const searchingAtom = atom('');
const searchResultAtom = atom(async (get) => {
  const count = get(searchingAtom);
  // simulate API delay
  await new Promise((resolve) => setTimeout(resolve, 1000));
  return count;
});

function App() {
  const [searchingWord, setSearchSuspense] = useAtom(searchingAtom);
  const [searchWord, setSearch] = useState(searchingWord);
  const [result] = useAtom(searchResultAtom);
  const [startTransition, isPending] = unstable_useTransition();
  return (
    <div className="App">
      <header className="App-header">
        <input
          type="text"
          value={searchWord}
          onInput={(e) => {
            setSearch(e.currentTarget.value);
            startTransition(() => {
              setSearchSuspense(e.currentTarget.value);
            });
          }}
        />
        <br />
        <span style={isPending ? { color: 'gray' } : undefined}>{result}</span>
      </header>
    </div>
  );
}

export default function () {
  return (
    <React.Suspense fallback="Loading...">
      <App />
    </React.Suspense>
  );
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, Apr 16, 2021

Oh, I thought this library is concurrent-mode-ready…

Well, it’s hard to define what is CM-ready. Anyway, @Jack-Works this is nice catch. I will investigate it further. Thanks for the codesandbox too.

As @Thisen noted, we don’t run our tests in the experimental build, so in one aspect it’s not yet supported. We do support <Suspense> in Legacy Mode. (But, note it’s technically unstable.) One the other hand, I run some experiments with https://github.com/dai-shi/will-this-react-global-state-work-in-concurrent-mode, and isPending seems to work fine for sync atoms. So, the issue is the async atom usage in Concurrent Mode. I’d say it’s something between a bug and a feature request.

I would like to find a proper usage, and possibly fix it. However, note that I don’t think this is blocking v1 release anyway. For example, if we fix it now, it may not work in the future version of React.

I’ll revisit the “contracts” and check the implementation. If anyone noticed anything, it would be good to share here.

0reactions
dai-shicommented, May 31, 2021

Let me close this for now and revisit it once React releases a new version.

jotai v1 won’t support CM officially, but it should work. And to my knowledge, the current behavior is correct (or best effort) at the moment.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Concurrent UI Patterns (Experimental) - React
First, we'll make sure that we're actually using Concurrent Mode. We'll talk more about ... We'll use it to tell React which state...
Read more >
How to use React's concurrent mode - InfoWorld
React's new concurrent mode allows your interface to be rendered while data fetching is in progress, providing an improved render lifecycle ...
Read more >
The Story of Concurrent React - YouTube
2161 days. That's how long the React team has been working on this release. This video tells that story.
Read more >
Concurrent Mode in React - Telerik
To solve that problem, concurrent mode offers Suspense. Suspense allows a component to suspend the render while a condition is not met, and ......
Read more >
Everything You Need to Know About React Concurrent Mode ...
Under no circumstances use this advice for production (as of May 2020). Concurrent Mode is experimental, full of bugs, and may or may...
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