-
Notifications
You must be signed in to change notification settings - Fork 143
Allow functions to be passed to setState #39
Allow functions to be passed to setState #39
Conversation
lib/Component.lua
Outdated
@@ -121,6 +121,11 @@ function Component:setState(partialState) | |||
error(INVALID_SETSTATE_MESSAGE, 0) | |||
end | |||
|
|||
-- If the partial state is a function, invoke it to get the actual partial state. | |||
if type(partialState) == "function" then | |||
partialState = partialState(self.props, self.state) |
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.
Let's flip-flop the arguments to match React. I think most cases of functional setState
are going to depend on state
more than props
anyhow.
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.
Makes sense; fixed now!
lib/Component.spec.lua
Outdated
|
||
expect(getStateCallback().value).to.equal(0) | ||
|
||
setStateCallback(function(props, state) |
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.
We might want to assert which argument is which in this function to guard against them being the wrong tables!
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.
Done!
Thanks! |
This fixes #33. It allows
setState
to accept a function, which is called with theprops
andstate
tables. This function is expected to return a partial state, which is then used to update the component's state.