Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Make PureComponent perform shallow equality check on state #377

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
@@ -1,6 +1,7 @@
# Roact Changelog

## Unreleased Changes
* Made PureComponent perform shallow equality check on state

## [1.4.4](https://github.com/Roblox/roact/releases/tag/v1.4.4) (June 13th, 2022)
* Added Luau analysis to the repository ([#372](https://github.com/Roblox/roact/pull/372))
Expand Down
18 changes: 12 additions & 6 deletions src/PureComponent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ local PureComponent = Component:extend("PureComponent")
PureComponent.extend = Component.extend

function PureComponent:shouldUpdate(newProps, newState)
-- In a vast majority of cases, if state updated, something has updated.
-- We don't bother checking in this case.
if newState ~= self.state then
return true
if (newProps == self.props) and (newState == self.state) then
return false
end

if newProps == self.props then
return false
for key, value in pairs(newState) do
if self.state[key] ~= value then
return true
end
end

for key, value in pairs(self.state) do
if newState[key] ~= value then
return true
end
end

for key, value in pairs(newProps) do
Expand Down