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

Commit

Permalink
Add getDerivedStateFromProps (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmaranthineCodices authored and LPGhatguy committed Apr 5, 2018
1 parent bbb0663 commit 5bad459
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 14 deletions.
57 changes: 43 additions & 14 deletions lib/Component.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ setState cannot be used currently, are you calling setState from any of:
* the render function
* the shouldUpdate function]]

local function mergeState(currentState, partialState)
local newState = {}

for key, value in pairs(currentState) do
newState[key] = value
end

for key, value in pairs(partialState) do
if value == Core.None then
newState[key] = nil
else
newState[key] = value
end
end

return newState
end

--[[
Create a new Roact stateful component class.
Expand Down Expand Up @@ -76,6 +94,14 @@ function Component:extend(name)
self.state = {}
end

if class.getDerivedStateFromProps then
local partialState = class.getDerivedStateFromProps(props, self.state)

if partialState then
self.state = mergeState(self.state, partialState)
end
end

-- Now that state has definitely been set, we can now allow it to be changed.
self._canSetState = true

Expand Down Expand Up @@ -133,20 +159,7 @@ function Component:setState(partialState)
partialState = partialState(self.state, self.props)
end

local newState = {}

for key, value in pairs(self.state) do
newState[key] = value
end

for key, value in pairs(partialState) do
if value == Core.None then
newState[key] = nil
else
newState[key] = value
end
end

local newState = mergeState(self.state, partialState)
self:_update(self.props, newState)
end

Expand Down Expand Up @@ -174,6 +187,22 @@ end
function Component:_forceUpdate(newProps, newState)
self._canSetState = false

-- Compute new derived state.
-- Get the class - getDerivedStateFromProps is static.
local class = getmetatable(self)

-- Only update if newProps are given!
if newProps then
if class.getDerivedStateFromProps then
local derivedState = class.getDerivedStateFromProps(newProps, newState or self.state)

-- getDerivedStateFromProps can return nil if no changes are necessary.
if derivedState ~= nil then
newState = mergeState(newState or self.state, derivedState)
end
end
end

if self.willUpdate then
self:willUpdate(newProps or self.props, newState or self.state)
end
Expand Down
39 changes: 39 additions & 0 deletions lib/Component.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,45 @@ return function()
Reconciler.teardown(instance)
end)

it("should call getDerivedStateFromProps appropriately", function()
local TestComponent = Component:extend("TestComponent")
local getStateCallback

function TestComponent.getDerivedStateFromProps(newProps, oldState)
return {
visible = newProps.visible
}
end

function TestComponent:init(props)
self.state = {
visible = false
}

getStateCallback = function()
return self.state
end
end

function TestComponent:render() end

local handle = Reconciler.reify(Core.createElement(TestComponent, {
visible = true
}))

local state = getStateCallback()
expect(state.visible).to.equal(true)

handle = Reconciler.reconcile(handle, Core.createElement(TestComponent, {
visible = 123
}))

state = getStateCallback()
expect(state.visible).to.equal(123)

Reconciler.teardown(handle)
end)

describe("setState", function()
it("should throw when called in init", function()
local InitComponent = Component:extend("InitComponent")
Expand Down

0 comments on commit 5bad459

Please sign in to comment.