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.

Support useTransition() for showing previous Recoil state with Recoil state changes.

See original GitHub issue

i realize that useTransition is bleeding edge, but i’d like to get an early handle on it. this far i’ve not been able to figure out any combination of parts to get useTransition to work, and i can’t find any examples online. all my imports work and i’m following the code like this.

    recoil: 0.1.1
    react: 0.0.0-experimental-4ead6b530
    react-dom: 0.0.0-experimental-4ead6b530

and using the concurrent pattern:

ReactDOM.unstable_createRoot(document.getElementById("root")).render(
  <BrowserRouter>
    <Route path="/" component={App} />
  </BrowserRouter>
);

and an otherwise typical and working <Suspense /> pattern:

import React, { Suspense, useState, unstable_useTransition as useTransition } from "react";
import { atom, selector, useRecoilState } from "recoil";

export const baseState = atom({
  key: "base",
  default: 100,
});

export const resourceState = selector({
  key: "resource",
  get: ({ get }) => {
    const base = get(baseState);
    return new Promise(resolve => {
      setTimeout(() => {
        resolve(base);
      }, 2000);
    });
  },
  set: ({ set }, value) => {
    set(baseState, value);
  },
});

const UseTransitionEample = () => {
  const [resource, setResource] = useRecoilState(resourceState);
  const [startTransition, isPending] = useTransition({ timeoutMs: 3000 });

  return (
    <>
      <div>resource: {resource}</div>
      <div>isPending: {isPending ? "true" :  "false"}</div>
      <button onClick={() =>
          startTransition(() => {
            setResource(resource + 1);
          })
      }>Update</button>
    </>
  );
}

const SuspenseWrapper = () => (
  <Suspense fallback={<div>"Loading..."</div>}>
    <UseTransitionEample />
  </Suspense>
);

export default SuspenseWrapper;

In the above code clicking the button results in the “Loading…” fallback being presented for the full duration of the “load”.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:29 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
drarmstrcommented, Jan 20, 2021

We’re discussing a possible fix for this now, likely related to a planned optimization as well.

1reaction
treborcommented, Apr 6, 2022

my mistaken understanding as per this description was that the original page content should be displayed only for timeoutMs time. hunting around i found this pr which removes that param. so yes i think recoil is doing the right thing now. thanks so much for the hand holding. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

React 18 Transitions - Recoil
React 18 offers a new hook useTransition() for transitioning to a new state while having control over what to render before the new...
Read more >
Concurrent UI Patterns (Experimental) - React
When we useTransition , React will let us “stay” on the previous screen — and show a progress indicator there. We call that...
Read more >
Recoil updating state when nothing has changed
I am trying to figure out what I need to do in order for it not to do API calls if nothing in...
Read more >
recoil | Yarn - Package Manager
Recoil is an experimental state management framework for React. ... with nested renderers that don't support useSyncExternalStore() (#2001, #2010) ...
Read more >
which state manager you are using with React 18? : r/reactjs
suspense) is non existant (tearing occurs) and ONLY useTransition() hook from react 18 is currently supported. I have checked mobx state tree ...
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