Skip to content

Commit 4795456

Browse files
committed
fix: reload hooks when hook body changes
1 parent a1c5c31 commit 4795456

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

Diff for: README.md

+5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ There is only one condition for it - a non zero dependencies list.
6767
🔥 useEffect(effect, ["hot"]); // the simplest way to make hook reloadable
6868
```
6969

70+
**Plus**
71+
72+
* any hook would be reloaded on a function body change. Enabled by default, controlled by `reloadHooksOnBodyChange` option.
73+
* you may configure RHL to reload any hook by setting `reloadLifeCycleHooks` option to true.
74+
7075
**To disable hooks reloading** - set configuration option:
7176

7277
```js

Diff for: src/configuration.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ const configuration = {
1111
// Allows SFC to be used, enables "intermediate" components used by Relay, should be disabled for Preact
1212
allowSFC: true,
1313

14-
// Allow hot reload of effect hooks
14+
// Allow reload of effect hooks with non zero dependency list
1515
reloadHooks: true,
1616

17+
// Allow reload of mount effect hooks - zero deps
18+
reloadLifeCycleHooks: false,
19+
20+
// Enables hook reload on hook body change
21+
reloadHooksOnBodyChange: true,
22+
1723
// Disable "hot-replacement-render"
1824
disableHotRenderer: false,
1925

Diff for: src/reactHotLoader.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,24 @@ const forceSimpleSFC = { proxy: { pureSFC: true } };
2929

3030
const hookWrapper = hook => {
3131
const wrappedHook = function(cb, deps) {
32-
if (configuration.reloadHooks) {
33-
return hook(cb, deps && deps.length > 0 ? [...deps, getHotGeneration()] : deps);
32+
if (configuration.reloadHooks && deps) {
33+
const inputs = [...deps];
34+
35+
// reload hooks which have changed string representation
36+
if (configuration.reloadHooksOnBodyChange) {
37+
inputs.push(String(cb));
38+
}
39+
40+
if (
41+
// reload hooks with dependencies
42+
deps.length > 0 ||
43+
// reload all hooks of option is set
44+
(configuration.reloadLifeCycleHooks && deps.length === 0)
45+
) {
46+
inputs.push(getHotGeneration());
47+
}
48+
49+
return hook(cb, inputs);
3450
}
3551
return hook(cb, deps);
3652
};

0 commit comments

Comments
 (0)