Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Universally Controlled Components #5

Open
kof opened this issue Nov 24, 2018 · 0 comments
Open

Universally Controlled Components #5

kof opened this issue Nov 24, 2018 · 0 comments

Comments

@kof
Copy link

kof commented Nov 24, 2018

Since react hooks more patterns emerge, one of these I called Universally Controlled Components. Originally tweeted by @gaearon https://twitter.com/oleg008/status/1065386273177776129

The idea is that we can build components in a way that allows us to control them from outside without using internal state as well as using it depending on props, without much work.

const [value, setValue] = useControlledState(props.value, props.onChange);

[props.value, props.onChange] if onChange is specified, local state otherwise.

A good use case for it could be styleguides and tests, where you might want to use internal state, but then use it in the application with a controlled state from the outside.

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

No branches or pull requests

1 participant