From 479545696362c8ddc225c43691b788810e8afc02 Mon Sep 17 00:00:00 2001 From: Anton Korzunov Date: Mon, 15 Jul 2019 20:17:30 +1000 Subject: [PATCH] fix: reload hooks when hook body changes --- README.md | 5 +++++ src/configuration.js | 8 +++++++- src/reactHotLoader.js | 20 ++++++++++++++++++-- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 697075663..5d4c7d903 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,11 @@ There is only one condition for it - a non zero dependencies list. 🔥 useEffect(effect, ["hot"]); // the simplest way to make hook reloadable ``` +**Plus** + +* any hook would be reloaded on a function body change. Enabled by default, controlled by `reloadHooksOnBodyChange` option. +* you may configure RHL to reload any hook by setting `reloadLifeCycleHooks` option to true. + **To disable hooks reloading** - set configuration option: ```js diff --git a/src/configuration.js b/src/configuration.js index 36dc2d401..de0bf8771 100644 --- a/src/configuration.js +++ b/src/configuration.js @@ -11,9 +11,15 @@ const configuration = { // Allows SFC to be used, enables "intermediate" components used by Relay, should be disabled for Preact allowSFC: true, - // Allow hot reload of effect hooks + // Allow reload of effect hooks with non zero dependency list reloadHooks: true, + // Allow reload of mount effect hooks - zero deps + reloadLifeCycleHooks: false, + + // Enables hook reload on hook body change + reloadHooksOnBodyChange: true, + // Disable "hot-replacement-render" disableHotRenderer: false, diff --git a/src/reactHotLoader.js b/src/reactHotLoader.js index a1ef1e94a..2254684b5 100644 --- a/src/reactHotLoader.js +++ b/src/reactHotLoader.js @@ -29,8 +29,24 @@ const forceSimpleSFC = { proxy: { pureSFC: true } }; const hookWrapper = hook => { const wrappedHook = function(cb, deps) { - if (configuration.reloadHooks) { - return hook(cb, deps && deps.length > 0 ? [...deps, getHotGeneration()] : deps); + if (configuration.reloadHooks && deps) { + const inputs = [...deps]; + + // reload hooks which have changed string representation + if (configuration.reloadHooksOnBodyChange) { + inputs.push(String(cb)); + } + + if ( + // reload hooks with dependencies + deps.length > 0 || + // reload all hooks of option is set + (configuration.reloadLifeCycleHooks && deps.length === 0) + ) { + inputs.push(getHotGeneration()); + } + + return hook(cb, inputs); } return hook(cb, deps); };