Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - "'value' prop on 'input' should not be null" for some input but not others

Tags:

reactjs

In my React app (version 15.5.4), I am getting the following warning for an input field in one of my components:

Warning: 'value' prop on 'input' should not be null. Consider using the empty string to clear the component or 'undefined' for uncontrolled components.

referring to the following jsx:

<label>Description<br />     <input         type="text"         name="description"         value={this.state.group.description}         onChange={this.handleChange}         maxLength="99" /> </label> 

But I am perplexed by this, because the value of this.state.group.description is set as "" in my constructor:

this.state = {     "group": {         "name": "",         "description": ""     } } 

and furthering my consternation is the fact that another input field refers to this.state.group.name, and yet no warning is thrown:

<label>Name *<br />     <input         type="text"         name="name"         value={this.state.group.name}         onChange={this.handleChange}         maxLength="99"         required="required"         autoFocus /> </label> 

Am I missing something here? As far as I can tell, I've set up the initial state of these two values as empty strings and referred to them in the same way in the two input fields, yet one throws a warning and one does not.

It's not the end of the world... the app works fine, but I'd really like to understand why this is happening and get my app running clean.

Here's the handleChange:

handleChange(event) {     const attribute = event.target.name     const updatedGroup = this.state.group     updatedGroup[attribute] = event.target.value     this.setState({"group": updatedGroup}) } 
like image 823
skwidbreth Avatar asked Aug 16 '17 18:08

skwidbreth


People also ask

Can props be null React?

To specify null prop type in React, we can set null as the default prop value. We create the MyComponent component accepts the item prop. We set item 's type to be a string and is required. And then we specify that its default value is null so it can be set to null when nothing is set for item .

How do you use React to set the value of an input?

To get the value of an input on button click in React: Declare a state variable that tracks the value of the input field. Add an onClick prop to a button element. When the button is clicked, update the state variable.

Can you modify the values of props?

You can't change the value of props but you still have options to change values that you can use. You have two options: Either you change it from the parent component or you use state instead of props. Where ChildComponent has your code to use this.props.name .

Can we assign value to props in React?

A component cannot update its own props unless they are arrays or objects (having a component update its own props even if possible is an anti-pattern), but can update its state and the props of its children.


1 Answers

Thanks to @ShubhamKhatri and @Dekel for pointing me in the right direction on this - I hadn't even considered the fact that the empty string set in the constructor was being overwritten with a problematic value. It turns out that the source of the issue was that after setting the value of description as an empty string, my API was overwriting it with null.

I resolved this by tweaking my render method like so:

let groupDescription;  if (!this.state.group.description) {     groupDescription = "" } else {     groupDescription = this.state.group.description }  return (     <label>Description<br />         <input             type="text"             name="description"             value={groupDescription}             onChange={this.handleChange}             maxLength="99" />     </label> ) 
like image 119
skwidbreth Avatar answered Oct 19 '22 08:10

skwidbreth