From e27c4dae01d7fe57627cf52d11eadcba3d18ef93 Mon Sep 17 00:00:00 2001 From: Christopher <51393127+chriscerie@users.noreply.github.com> Date: Sat, 19 Mar 2022 11:10:32 -0700 Subject: [PATCH 1/4] Fix useEffect not running when a dependency changes to nil --- src/init.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/init.lua b/src/init.lua index 341b2a4..56b49b0 100644 --- a/src/init.lua +++ b/src/init.lua @@ -81,14 +81,22 @@ function Hooks.new(roact) local lastDependencies = self.effectDependencies[index] if lastDependencies ~= nil then local anythingChanged = false - + local length = 0 for dependencyIndex, dependency in pairs(dependsOn) do + length += 1 if lastDependencies[dependencyIndex] ~= dependency then anythingChanged = true break end end + for _, _ in pairs(lastDependencies) do + length -= 1 + end + if length ~= 0 then + anythingChanged = true + end + if not anythingChanged then continue end From 1a4151a52a5057e3a9c8907c5aa442cabc4fad95 Mon Sep 17 00:00:00 2001 From: Christopher <51393127+chriscerie@users.noreply.github.com> Date: Sat, 19 Mar 2022 11:22:58 -0700 Subject: [PATCH 2/4] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1261abb..26d78ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fix useContext for uninitialized and/or changing contexts (#26) - `useMemo` now computes a new value every render if no array is provided (#29) +- Fixed `useEffect` not running when a dependency changes to nil (#32) ## [0.3.0] ### Changed From c60d399ee338b633a5a469a4ba558b5700889d72 Mon Sep 17 00:00:00 2001 From: boyned//Kampfkarren Date: Tue, 5 Apr 2022 14:15:13 -0700 Subject: [PATCH 3/4] Better whitespace --- src/init.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/init.lua b/src/init.lua index 56b49b0..9868a6a 100644 --- a/src/init.lua +++ b/src/init.lua @@ -82,8 +82,10 @@ function Hooks.new(roact) if lastDependencies ~= nil then local anythingChanged = false local length = 0 + for dependencyIndex, dependency in pairs(dependsOn) do length += 1 + if lastDependencies[dependencyIndex] ~= dependency then anythingChanged = true break @@ -93,6 +95,7 @@ function Hooks.new(roact) for _, _ in pairs(lastDependencies) do length -= 1 end + if length ~= 0 then anythingChanged = true end From ab14cdd7f422ecbe90cacb3eefc68c432152bd2e Mon Sep 17 00:00:00 2001 From: boyned//Kampfkarren Date: Tue, 5 Apr 2022 14:15:39 -0700 Subject: [PATCH 4/4] Better for-loop --- src/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/init.lua b/src/init.lua index 9868a6a..14ca9a1 100644 --- a/src/init.lua +++ b/src/init.lua @@ -92,7 +92,7 @@ function Hooks.new(roact) end end - for _, _ in pairs(lastDependencies) do + for _ in pairs(lastDependencies) do length -= 1 end