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

Allow canceling render #64

Merged
merged 2 commits into from
Apr 11, 2018
Merged
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
5 changes: 5 additions & 0 deletions lib/Component.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ function Component:setState(partialState)
-- If the partial state is a function, invoke it to get the actual partial state.
if type(partialState) == "function" then
partialState = partialState(self.state, self.props)

-- If partialState is nil, abort the render.
if partialState == nil then
return
end
end

local newState = {}
Expand Down
33 changes: 33 additions & 0 deletions lib/Component.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -374,5 +374,38 @@ return function()

Reconciler.teardown(instance)
end)

it("should cancel rendering if the function returns nil", function()
local TestComponent = Component:extend("TestComponent")
local setStateCallback
local renderCount = 0

function TestComponent:init()
setStateCallback = function(newState)
self:setState(newState)
end

self.state = {
value = 0
}
end

function TestComponent:render()
renderCount = renderCount + 1
return nil
end

local element = Core.createElement(TestComponent)
local instance = Reconciler.reify(element)
expect(renderCount).to.equal(1)

setStateCallback(function(state, props)
return nil
end)

expect(renderCount).to.equal(1)

Reconciler.teardown(instance)
end)
end)
end