Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Fix useEffect not running when a dependency changes to nil #32

Merged
merged 4 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,25 @@ function Hooks.new(roact)
local lastDependencies = self.effectDependencies[index]
if lastDependencies ~= nil then
local anythingChanged = false
local length = 0
Kampfkarren marked this conversation as resolved.
Show resolved Hide resolved

for dependencyIndex, dependency in pairs(dependsOn) do
length += 1
Kampfkarren marked this conversation as resolved.
Show resolved Hide resolved

if lastDependencies[dependencyIndex] ~= dependency then
anythingChanged = true
break
end
end

for _, _ in pairs(lastDependencies) do
Kampfkarren marked this conversation as resolved.
Show resolved Hide resolved
length -= 1
end
Kampfkarren marked this conversation as resolved.
Show resolved Hide resolved

if length ~= 0 then
anythingChanged = true
end

if not anythingChanged then
continue
end
Expand Down