Skip to content

Commit

Permalink
fix: reload hooks when hook body changes
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Jul 15, 2019
1 parent a1c5c31 commit 4795456
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
20 changes: 18 additions & 2 deletions src/reactHotLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down

0 comments on commit 4795456

Please sign in to comment.