Skip to content

Commit a75b93f

Browse files
gaearonrickhanlonii
authored andcommitted
Report refreshed families to the caller (facebook#15957)
1 parent dc1bb33 commit a75b93f

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

packages/react-refresh/src/ReactFreshRuntime.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ function resolveFamily(type) {
142142
return familiesByType.get(type);
143143
}
144144

145-
export function performReactRefresh(): boolean {
145+
export function performReactRefresh(): RefreshUpdate | null {
146146
if (__DEV__) {
147147
if (pendingUpdates.length === 0) {
148-
return false;
148+
return null;
149149
}
150150

151151
const staleFamilies = new Set();
@@ -169,9 +169,10 @@ export function performReactRefresh(): boolean {
169169
}
170170
});
171171

172+
// TODO: rename these fields to something more meaningful.
172173
const update: RefreshUpdate = {
173-
updatedFamilies,
174-
staleFamilies,
174+
updatedFamilies, // Families that will re-render preserving state
175+
staleFamilies, // Families that will be remounted
175176
};
176177

177178
if (typeof setRefreshHandler !== 'function') {
@@ -182,7 +183,7 @@ export function performReactRefresh(): boolean {
182183
'called before the global DevTools hook was set up, or after the ' +
183184
'renderer has already initialized. Please file an issue with a reproducing case.',
184185
);
185-
return false;
186+
return null;
186187
}
187188

188189
if (typeof scheduleRefresh !== 'function') {
@@ -193,7 +194,7 @@ export function performReactRefresh(): boolean {
193194
'called before the global DevTools hook was set up, or after the ' +
194195
'renderer has already initialized. Please file an issue with a reproducing case.',
195196
);
196-
return false;
197+
return null;
197198
}
198199
const scheduleRefreshForRoot = scheduleRefresh;
199200

@@ -217,7 +218,7 @@ export function performReactRefresh(): boolean {
217218
if (didError) {
218219
throw firstError;
219220
}
220-
return true;
221+
return update;
221222
} else {
222223
throw new Error(
223224
'Unexpected call to React Refresh in a production environment.',

packages/react-refresh/src/__tests__/ReactFresh-test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3266,4 +3266,35 @@ describe('ReactFresh', () => {
32663266
)(global, React, ReactFreshRuntime, expect, createReactClass);
32673267
}
32683268
});
3269+
3270+
it('reports updated and remounted families to the caller', () => {
3271+
if (__DEV__) {
3272+
const HelloV1 = () => {
3273+
const [val, setVal] = React.useState(0);
3274+
return (
3275+
<p style={{color: 'blue'}} onClick={() => setVal(val + 1)}>
3276+
{val}
3277+
</p>
3278+
);
3279+
};
3280+
$RefreshReg$(HelloV1, 'Hello');
3281+
3282+
const HelloV2 = () => {
3283+
const [val, setVal] = React.useState(0);
3284+
return (
3285+
<p style={{color: 'red'}} onClick={() => setVal(val + 1)}>
3286+
{val}
3287+
</p>
3288+
);
3289+
};
3290+
$RefreshReg$(HelloV2, 'Hello');
3291+
3292+
const update = ReactFreshRuntime.performReactRefresh();
3293+
expect(update.updatedFamilies.size).toBe(1);
3294+
expect(update.staleFamilies.size).toBe(0);
3295+
const family = update.updatedFamilies.values().next().value;
3296+
expect(family.current.name).toBe('HelloV2');
3297+
// For example, we can use this to print a log of what was updated.
3298+
}
3299+
});
32693300
});

packages/react-refresh/src/__tests__/ReactFreshIntegration-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ describe('ReactFreshIntegration', () => {
8282
ReactDOM.render(<Component />, container);
8383
});
8484
// Module initialization shouldn't be counted as a hot update.
85-
expect(ReactFreshRuntime.performReactRefresh()).toBe(false);
85+
expect(ReactFreshRuntime.performReactRefresh()).toBe(null);
8686
}
8787

8888
function patch(source) {
8989
execute(source);
9090
act(() => {
91-
expect(ReactFreshRuntime.performReactRefresh()).toBe(true);
91+
expect(ReactFreshRuntime.performReactRefresh()).not.toBe(null);
9292
});
9393
expect(ReactFreshRuntime._getMountedRootCount()).toBe(1);
9494
}

0 commit comments

Comments
 (0)