Skip to content

Commit

Permalink
fix: forcely update Context Provider, fixes #1207
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Jul 22, 2019
1 parent a5597c6 commit 897a68d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/reconciler/componentComparator.js
Expand Up @@ -153,10 +153,14 @@ const compareComponents = (oldType, newType, setNewType, baseType) => {
}

if (isLazyType({ type: oldType })) {
// no need to update
// setNewType(newType);
return defaultResult;
}

if (isContextType({ type: oldType })) {
// update provider
setNewType(newType);
return defaultResult;
}

Expand Down
58 changes: 58 additions & 0 deletions test/hot/react-dom.integration.spec.js
Expand Up @@ -101,6 +101,64 @@ describe(`🔥-dom`, () => {
expect(unmount).toHaveBeenCalled();
});

it('should reload context', async () => {
const mount = jest.fn();
const unmount = jest.fn();

const genApp = contextValue => {
const context = React.createContext(contextValue);

const RenderContext = () => {
const v = React.useContext(context);

return <span>contextValue={v}</span>;
};

const MountCheck = () => {
React.useEffect(() => {
mount('test1');
return unmount;
}, []);
return 'fun1';
};

const App = () => (
<div>
<RenderContext />
<context.Provider value={`~${contextValue}~`}>
<RenderContext />
<MountCheck />
</context.Provider>
</div>
);

ReactHotLoader.register(context, 'context', 'test');

return App;
};

const el = document.createElement('div');
const App1 = genApp('1-test-1');
ReactDom.render(<App1 />, el);

expect(el.innerHTML).toMatch(/1-test-1/);
expect(el.innerHTML).toMatch(/~1-test-1~/);

incrementHotGeneration();
{
const App1 = genApp('2-hot-2');
ReactDom.render(<App1 />, el);
}

await tick();

expect(el.innerHTML).toMatch(/2-hot-2/);
expect(el.innerHTML).toMatch(/~2-hot-2~/);

expect(mount).toHaveBeenCalledTimes(1);
expect(unmount).toHaveBeenCalledTimes(0);
});

it('should reload hook effect', async () => {
const mount = jest.fn();
const unmount = jest.fn();
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Expand Up @@ -4594,11 +4594,6 @@ lodash@^4.0.0, lodash@^4.13.1, lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.4, l
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
integrity sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=

lodash@^4.17.11:
version "4.17.11"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==

lodash@^4.17.5:
version "4.17.5"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511"
Expand Down

0 comments on commit 897a68d

Please sign in to comment.