Skip to content
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

Fix: Optimistic update does not get reset #27453

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1817,9 +1817,9 @@ function updateOptimisticImpl<S, A>(
// as an argument. It's called a passthrough because if there are no pending
// updates, it will be returned as-is.
//
// Reset the base state and memoized state to the passthrough. Future
// updates will be applied on top of this.
hook.baseState = hook.memoizedState = passthrough;
// Reset the base state to the passthrough. Future updates will be applied
// on top of this.
hook.baseState = passthrough;

// If a reducer is not provided, default to the same one used by useState.
const resolvedReducer: (S, A) => S =
Expand Down Expand Up @@ -1853,9 +1853,9 @@ function rerenderOptimistic<S, A>(

// This is a mount. No updates to process.

// Reset the base state and memoized state to the passthrough. Future
// updates will be applied on top of this.
hook.baseState = hook.memoizedState = passthrough;
// Reset the base state to the passthrough. Future updates will be applied
// on top of this.
hook.baseState = passthrough;
const dispatch = hook.queue.dispatch;
return [passthrough, dispatch];
}
Expand Down
63 changes: 63 additions & 0 deletions packages/react-reconciler/src/__tests__/ReactAsyncActions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1112,4 +1112,67 @@ describe('ReactAsyncActions', () => {
</>,
);
});

// @gate enableAsyncActions
test('useOptimistic can update repeatedly in the same async action', async () => {
let startTransition;
let setLoadingProgress;
let setText;
function App() {
const [, _startTransition] = useTransition();
const [text, _setText] = useState('A');
const [loadingProgress, _setLoadingProgress] = useOptimistic(0);
startTransition = _startTransition;
setText = _setText;
setLoadingProgress = _setLoadingProgress;

return (
<>
{loadingProgress !== 0 ? (
<div key="progress">
<Text text={`Loading... (${loadingProgress})`} />
</div>
) : null}
<div key="real">
<Text text={text} />
</div>
</>
);
}

// Initial render
const root = ReactNoop.createRoot();
await act(() => root.render(<App text="A" />));
assertLog(['A']);
expect(root).toMatchRenderedOutput(<div>A</div>);

await act(async () => {
startTransition(async () => {
setLoadingProgress('25%');
await getText('Wait 1');
setLoadingProgress('75%');
await getText('Wait 2');
startTransition(() => setText('B'));
});
});
assertLog(['Loading... (25%)', 'A']);
expect(root).toMatchRenderedOutput(
<>
<div>Loading... (25%)</div>
<div>A</div>
</>,
);

await act(() => resolveText('Wait 1'));
assertLog(['Loading... (75%)', 'A']);
expect(root).toMatchRenderedOutput(
<>
<div>Loading... (75%)</div>
<div>A</div>
</>,
);

await act(() => resolveText('Wait 2'));
assertLog(['B']);
});
});