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.

Controller requires it's props for custom components

See original GitHub issue

Describe the bug When using Controller with a custom input component, causes the Controller to require the value and onChange props to be required.

To Reproduce Steps to reproduce the behavior:

  1. Open codesandbox
  2. Observe the type errors

Codesandbox link (Required) https://codesandbox.io/s/react-hook-form-controller-template-qrtco

Expected behavior I would expect the typings to be smart enough to know if I have already passed the values or to know that the properties are injected.

Additional context Also appears to be an issue with <input name="test" />

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:20 (19 by maintainers)

github_iconTop GitHub Comments

2reactions
stramelcommented, May 27, 2020

So Bill posted the link I shared with him using the example of react-router v6 and their reasoning behind switching to an element based api instead of component.

I think we can offer a single prop as/render/element to handle the use-cases. Offering multiple props will just cause confusion.

For that single prop, we should be able to support render props or an element.

// Supported
<Controller name="test" control={control} render={<TextFields myUniqueProp={state} />} />
<Controller name="test" control={control} render={({onChange, onBlur, ...props}) => <TextFields handleChange={onChange} handleBlur={onBlur} {..props} />} />

// Not Supported
<Controller name="test" control={control} render={TextFields} /> // -> <Controller name="test" control={control} render={<TextFields />} />
<Controller name="test" control={control} render='input' /> // -> <Controller name="test" control={control} render={<input />} />

We then don’t have to worry about passing props through (ie. myUniqueProp) or handling conversion of their prop names (ie. onBlurKey).

The main issue that I see with this currently is instantiating the component without the value and onChange props. Unless we required the render prop pass through. (props) => <TextFields {...props} /> or we might be able to do some typescript magic with an interface as I alluded to earlier.

2reactions
stramelcommented, May 23, 2020

But that’s the issue is the DX isn’t good the way it currently stands.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Customize React Components with Props
In this tutorial, you'll create custom components by passing props to your component. Props are arguments that you provide to a JSX element....
Read more >
Controller | React Hook Form - Simple React forms validation
This is a render prop. A function that returns a React element and provides the ability to attach events and value into the...
Read more >
Turn Anything Into A Form Field With React Hook Form ...
Here we see the two props required to make our field component work with the Controller: value - It should show the current...
Read more >
React native custom component props not working
But in your AppTextInput component, you are only handling two props, icon and placeholder . To make the other TextInput props work, you...
Read more >
Custom Controls · React Redux Form
The mapping standard props to custom controls can be specified by the type of <Control> component you are using: // custom radio button...
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