Skip to content

Commit 897a68d

Browse files
committed
fix: forcely update Context Provider, fixes #1207
1 parent a5597c6 commit 897a68d

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

Diff for: src/reconciler/componentComparator.js

+4
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,14 @@ const compareComponents = (oldType, newType, setNewType, baseType) => {
153153
}
154154

155155
if (isLazyType({ type: oldType })) {
156+
// no need to update
157+
// setNewType(newType);
156158
return defaultResult;
157159
}
158160

159161
if (isContextType({ type: oldType })) {
162+
// update provider
163+
setNewType(newType);
160164
return defaultResult;
161165
}
162166

Diff for: test/hot/react-dom.integration.spec.js

+58
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,64 @@ describe(`🔥-dom`, () => {
101101
expect(unmount).toHaveBeenCalled();
102102
});
103103

104+
it('should reload context', async () => {
105+
const mount = jest.fn();
106+
const unmount = jest.fn();
107+
108+
const genApp = contextValue => {
109+
const context = React.createContext(contextValue);
110+
111+
const RenderContext = () => {
112+
const v = React.useContext(context);
113+
114+
return <span>contextValue={v}</span>;
115+
};
116+
117+
const MountCheck = () => {
118+
React.useEffect(() => {
119+
mount('test1');
120+
return unmount;
121+
}, []);
122+
return 'fun1';
123+
};
124+
125+
const App = () => (
126+
<div>
127+
<RenderContext />
128+
<context.Provider value={`~${contextValue}~`}>
129+
<RenderContext />
130+
<MountCheck />
131+
</context.Provider>
132+
</div>
133+
);
134+
135+
ReactHotLoader.register(context, 'context', 'test');
136+
137+
return App;
138+
};
139+
140+
const el = document.createElement('div');
141+
const App1 = genApp('1-test-1');
142+
ReactDom.render(<App1 />, el);
143+
144+
expect(el.innerHTML).toMatch(/1-test-1/);
145+
expect(el.innerHTML).toMatch(/~1-test-1~/);
146+
147+
incrementHotGeneration();
148+
{
149+
const App1 = genApp('2-hot-2');
150+
ReactDom.render(<App1 />, el);
151+
}
152+
153+
await tick();
154+
155+
expect(el.innerHTML).toMatch(/2-hot-2/);
156+
expect(el.innerHTML).toMatch(/~2-hot-2~/);
157+
158+
expect(mount).toHaveBeenCalledTimes(1);
159+
expect(unmount).toHaveBeenCalledTimes(0);
160+
});
161+
104162
it('should reload hook effect', async () => {
105163
const mount = jest.fn();
106164
const unmount = jest.fn();

Diff for: yarn.lock

-5
Original file line numberDiff line numberDiff line change
@@ -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
45944594
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
45954595
integrity sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=
45964596

4597-
lodash@^4.17.11:
4598-
version "4.17.11"
4599-
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
4600-
integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==
4601-
46024597
lodash@^4.17.5:
46034598
version "4.17.5"
46044599
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511"

0 commit comments

Comments
 (0)