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

Allow functions to be passed to setState #39

Merged

Conversation

AmaranthineCodices
Copy link
Contributor

This fixes #33. It allows setState to accept a function, which is called with the props and state tables. This function is expected to return a partial state, which is then used to update the component's state.

@coveralls
Copy link

coveralls commented Mar 5, 2018

Coverage Status

Coverage increased (+0.06%) to 84.979% when pulling 632dbcb on AmaranthineCodices:setState-function-variant into bb60936 on Roblox:master.

@@ -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)
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense; fixed now!


expect(getStateCallback().value).to.equal(0)

setStateCallback(function(props, state)
Copy link
Contributor

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!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@LPGhatguy LPGhatguy merged commit 85e1a49 into Roblox:master Mar 5, 2018
@LPGhatguy
Copy link
Contributor

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce setState(fn) variant
3 participants