Skip to content

Commit

Permalink
Update Context.Consumer inside suspended Suspense component
Browse files Browse the repository at this point in the history
  • Loading branch information
okmttdhr committed May 3, 2021
1 parent 79740da commit e03c92e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
4 changes: 0 additions & 4 deletions packages/react-reconciler/src/ReactFiberNewContext.new.js
Expand Up @@ -157,10 +157,6 @@ export function scheduleWorkOnParentPath(
!isSubsetOfLanes(alternate.childLanes, renderLanes)
) {
alternate.childLanes = mergeLanes(alternate.childLanes, renderLanes);
} else {
// Neither alternate was updated, which means the rest of the
// ancestor path already has sufficient priority.
break;
}
node = node.return;
}
Expand Down
Expand Up @@ -1523,4 +1523,63 @@ describe('ReactSuspense', () => {
expect(root).toMatchRenderedOutput('new value');
});
});

it('updates context consumer within child of suspended suspense component when context updates', () => {
const {createContext, useState} = React;

const ValueContext = createContext(null);

const promiseThatNeverResolves = new Promise(() => {});
function Child() {
return (
<ValueContext.Consumer>
{value => {
Scheduler.unstable_yieldValue(`Received context value [${value}]`);
if (value === 'default') return <Text text="default" />;
throw promiseThatNeverResolves;
}}
</ValueContext.Consumer>
);
}

let setValue;
function Wrapper({children}) {
const [value, _setValue] = useState('default');
setValue = _setValue;
return (
<ValueContext.Provider value={value}>{children}</ValueContext.Provider>
);
}

function App() {
return (
<Wrapper>
<Suspense fallback={<Text text="Loading..." />}>
<Child />
</Suspense>
</Wrapper>
);
}

const root = ReactTestRenderer.create(<App />);
expect(Scheduler).toHaveYielded([
'Received context value [default]',
'default',
]);
expect(root).toMatchRenderedOutput('default');

act(() => setValue('new value'));
expect(Scheduler).toHaveYielded([
'Received context value [new value]',
'Loading...',
]);
expect(root).toMatchRenderedOutput('Loading...');

act(() => setValue('default'));
expect(Scheduler).toHaveYielded([
'Received context value [default]',
'default',
]);
expect(root).toMatchRenderedOutput('default');
});
});

0 comments on commit e03c92e

Please sign in to comment.