diff --git a/lib/Component.lua b/lib/Component.lua index 7f412e2b..02411904 100644 --- a/lib/Component.lua +++ b/lib/Component.lua @@ -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 = {} diff --git a/lib/Component.spec.lua b/lib/Component.spec.lua index b5a7a0e5..46009560 100644 --- a/lib/Component.spec.lua +++ b/lib/Component.spec.lua @@ -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