This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
setState sanity checking #23
Merged
LPGhatguy
merged 4 commits into
Roblox:master
from
AmaranthineCodices:setState-sanity-check
Jan 29, 2018
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1e0130a
throw better errors when calling setState in wrong places
AmaranthineCodices cbf6a59
spaces -> tabs
AmaranthineCodices 7674b8b
make error message reflect that only setState is blocked
AmaranthineCodices 4683cb5
split init/render test cases
AmaranthineCodices File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,4 +173,56 @@ return function() | |
|
||
Reconciler.teardown(instance) | ||
end) | ||
|
||
describe("setState", function() | ||
it("should throw when called in init or render", function() | ||
local InitComponent = Component:extend("InitComponent") | ||
|
||
function InitComponent:init() | ||
self:setState({ | ||
a = 1 | ||
}) | ||
end | ||
|
||
local RenderComponent = Component:extend("RenderComponent") | ||
|
||
function RenderComponent:render() | ||
self:setState({ | ||
a = 1 | ||
}) | ||
end | ||
|
||
local initElement = Core.createElement(InitComponent) | ||
local renderElement = Core.createElement(RenderComponent) | ||
|
||
expect(function() | ||
Reconciler.reify(initElement) | ||
end).to.throw() | ||
|
||
expect(function() | ||
Reconciler.reify(renderElement) | ||
end).to.throw() | ||
end) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can break this one into two tests, just like how |
||
|
||
it("should throw when called in willUnmount", function() | ||
local TestComponent = Component:extend("TestComponent") | ||
|
||
function TestComponent:render() | ||
return nil | ||
end | ||
|
||
function TestComponent:willUnmount() | ||
self:setState({ | ||
a = 1 | ||
}) | ||
end | ||
|
||
local element = Core.createElement(TestComponent) | ||
local instance = Reconciler.reify(element) | ||
|
||
expect(function() | ||
Reconciler.teardown(instance) | ||
end).to.throw() | ||
end) | ||
end) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,9 @@ function Reconciler.teardown(instanceHandle) | |
Reconciler.teardown(instanceHandle._reified) | ||
end | ||
elseif Core.isStatefulElement(element) then | ||
-- Stop the component from setting state in willUnmount or anywhere thereafter. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, there are spaces for indentation here! |
||
instanceHandle._instance._canSetState = false | ||
|
||
-- Tell the component we're about to tear everything down. | ||
-- This gives it some notice! | ||
if instanceHandle._instance.willUnmount then | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be a little clearer to say
are you calling setState from
instead ofare you setting state from
, since you can you set state frominit
as long as you don't callsetState
itself!