Clients/Client2018/content/LuaPackages/RoactImpl/PureComponent.lua

41 lines
942 B
Lua

--[[
A version of Component with a `shouldUpdate` method that forces the
resulting component to be pure.
]]
local Component = require(script.Parent.Component)
local PureComponent = Component:extend("PureComponent")
-- When extend()ing a component, you don't get an extend method.
-- This is to promote composition over inheritance.
-- PureComponent is an exception to this rule.
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
end
if newProps == self.props then
return false
end
for key, value in pairs(newProps) do
if self.props[key] ~= value then
return true
end
end
for key, value in pairs(self.props) do
if newProps[key] ~= value then
return true
end
end
return false
end
return PureComponent