question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Built-in component for controlled components

See original GitHub issue

Disclaimer: I’m just now reading through the docs, maybe I have missed it. Apologies in advance 😃

Since React 16.4, having mixed-control components (that work either via a value prop and onChange callback, or with an internal state) are discouraged, if cumbersome to build.

I wonder if there could be a built-in component that can manage the state for a controlled component:

<Controller>
{ (state, setState) => 
    <MyComponent value={state.value} onChange={ value => setState({ value }) } />
}
</Controller>

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
denisborovikovcommented, Jun 12, 2018

Have you seen react-powerplug?

1reaction
jedrichardscommented, Oct 16, 2018

Wouldn’t an api like the below make Playground much more useful out of the box?

<Playground state={{value: ''}}>
  {
    ({state, setState}) => (
      <input
        value={state.value},
        onChange={e => setState({ value: e.target.value })}
      />
    )
  }
</Playground>

It would be completely optional and opt-in (i.e. using a render prop as opposed to children). Should be super easy to implement with react-powerplug under the hood.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating a React controlled components pattern custom Hook
How to create a custom React Hook that allows components to be both controlled and uncontrolled just like form elements.
Read more >
Creating React Components that can be Controlled and ...
A React component is controlled if you pass it the current state of its value as a prop. You will then usually also...
Read more >
Uncontrolled Components - React
In a controlled component, form data is handled by a React component. The alternative is uncontrolled components, where form data is handled by...
Read more >
Controlled and Uncontrolled Components in React
As per React docs — “In a controlled component, form data is handled by a React component. The alternative is uncontrolled components, ...
Read more >
Controlled Components (How To) - Treehouse
To manage an input field's state, we need to build a "controlled component." A controlled component renders a form element whose values are ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found