Skip to content

Current behavior for use resulting in stuck isPending #29209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions packages/react-reconciler/src/__tests__/ReactUse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ let act;
let use;
let useDebugValue;
let useState;
let useTransition;
let useMemo;
let useEffect;
let Suspense;
Expand All @@ -38,6 +39,7 @@ describe('ReactUse', () => {
use = React.use;
useDebugValue = React.useDebugValue;
useState = React.useState;
useTransition = React.useTransition;
useMemo = React.useMemo;
useEffect = React.useEffect;
Suspense = React.Suspense;
Expand Down Expand Up @@ -1915,4 +1917,62 @@ describe('ReactUse', () => {
assertLog(['Hi', 'World']);
expect(root).toMatchRenderedOutput(<div>Hi World</div>);
});

it('does not get stuck in pending state with usable values in state', async () => {
const initial = new Promise(resolve => {
setTimeout(() => {
resolve('initial');
}, 1000);
});
let click;
function Reader({promise, setPromise}) {
const value = use(promise);
const [isPending, startLocalTransition] = useTransition();
click = () => {
startLocalTransition(() => {
setPromise(
Comment on lines +1930 to +1933
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const [isPending, startLocalTransition] = useTransition();
click = () => {
startLocalTransition(() => {
setPromise(
const [isPending, setIsPending] = useOptimistic(false);
click = () => {
startTransition(() => {
setIsPending(true);
setPromise(

does work.

new Promise(resolve => {
setTimeout(() => {
resolve('updated');
}, 1000);
}),
);
});
};

Scheduler.log(`Value: ${value}, Pending: ${String(isPending)}`);

return (
<>
Value: {value}, Pending: {String(isPending)}
</>
);
}
function App() {
const [promise, setPromise] = useState(initial);

return (
<Suspense fallback="Loading">
<Reader promise={promise} setPromise={setPromise} />
</Suspense>
);
}

const root = ReactNoop.createRoot();
await act(() => {
root.render(<App />);
});
assertLog(['Value: initial, Pending: false']);
expect(root).toMatchRenderedOutput('Value: initial, Pending: false');

await act(() => {
click();
});

assertLog([
'Value: initial, Pending: true',
'Value: updated, Pending: true',
]);
expect(root).toMatchRenderedOutput('Value: updated, Pending: true');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We commit the value from the updated, resolved promise but don't flip isPending.

});
});