-
Notifications
You must be signed in to change notification settings - Fork 143
setState sanity checking #23
setState sanity checking #23
Conversation
expect(function() | ||
Reconciler.reify(renderElement) | ||
end).to.throw() | ||
end) |
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.
I think we can break this one into two tests, just like how willUnmount
is a separate test
lib/Reconciler.lua
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, there are spaces for indentation here!
lib/Component.lua
Outdated
-- * During the component's render function | ||
-- * After the component has been unmounted (or is in the process of unmounting, e.g. willUnmount) | ||
if not self._canSetState then | ||
error("State cannot be set at this point: are you setting state from an init, render, or willUnmount function?", 0) |
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 of are you setting state from
, since you can you set state from init
as long as you don't call setState
itself!
Thanks! |
Oops, I should've asked you to add an entry to the changelog! I'll go ahead and do that for both of these PRs. |
I know we've already merged this, but are there any other places where it might be inappropriate to call I reviewed some code today where |
One place that immediately comes to mind is
|
This builds on the discussion from #23 after it was merged. It disallows calling setState in all lifecycle hooks, not just willUnmount.
This fixes #17. It throws an error when setState is called in the following locations:
init
function of a componentrender
function of a componentwillUnmount
The error message:
Tests are included, yay!
This will probably have a merge conflict with #21; I'll fix it when it happens. This required an edit to
Reconciler
to set the flag to false just before unmounting the component.