Skip to content

Commit

Permalink
Refactor: use forceReset to remount unknown Hooks
Browse files Browse the repository at this point in the history
We already have the logic to reset a component, so let's just reuse it instead of that special case.
  • Loading branch information
gaearon committed Jun 5, 2019
1 parent 4cf58b0 commit f254e3a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 11 deletions.
6 changes: 3 additions & 3 deletions packages/react-fresh/src/ReactFreshBabelPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ export default function(babel) {
function createArgumentsForSignature(node, signature, scope) {
const {key, customHooks} = signature;

let forceReset = hasForceResetComment(scope.path);
let customHooksInScope = [];
customHooks.forEach(callee => {
// Check if a correponding binding exists where we emit the signature.
Expand All @@ -268,13 +269,12 @@ export default function(babel) {
customHooksInScope.push(callee);
} else {
// We don't have anything to put in the array because Hook is out of scope.
// But we can still mark that it exists. This will cause remount on edits.
customHooksInScope.push(null);
// Since it could potentially have been edited, remount the component.
forceReset = true;
}
});

const args = [node, t.stringLiteral(key)];
const forceReset = hasForceResetComment(scope.path);
if (forceReset || customHooksInScope.length > 0) {
args.push(t.booleanLiteral(forceReset));
}
Expand Down
6 changes: 0 additions & 6 deletions packages/react-fresh/src/ReactFreshRuntime.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ function haveEqualSignatures(prevType, nextType) {
}

for (let i = 0; i < nextCustomHooks.length; i++) {
if (prevCustomHooks[i] === undefined || nextCustomHooks[i] === undefined) {
// We used a custom Hook, but it isn't in the scope of the signature.
// For example this would happen if you define a Hook inline in the component.
// This is an edge case, but we'll treat it as always different signature.
return false;
}
if (!haveEqualSignatures(prevCustomHooks[i], nextCustomHooks[i])) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default function App() {
return foo;
}
__signature__(useFancyState, 'useState{[foo, setFoo]}\\nuseFancyEffect{}', false, () => [,]);
__signature__(useFancyState, 'useState{[foo, setFoo]}\\nuseFancyEffect{}', true);
const bar = useFancyState();
const baz = FancyHook.useThing();
Expand All @@ -76,7 +76,7 @@ export default function App() {
return <h1>{bar}{baz}</h1>;
}
__signature__(App, 'useFancyState{bar}\\nuseThing{baz}\\nuseState{}\\nuseThePlatform{}', false, () => [, FancyHook.useThing,,]);
__signature__(App, 'useFancyState{bar}\\nuseThing{baz}\\nuseState{}\\nuseThePlatform{}', true, () => [FancyHook.useThing]);
_c = App;
Expand Down

0 comments on commit f254e3a

Please sign in to comment.