Movatterモバイル変換


[0]ホーム

URL:


Form elements should be either controlled or uncontrolled

  • REACT_MISUSED_CONTROLLED_COMPONENT
  • Error
  • Medium
  • react

This rule applies when a form element such as<input>,<textarea>, and<select> works as both controlled and uncontrolled element.

The followings are controlled elements whose values are controlled by React state and associated handler functions:

  1. A form element hasvalue prop.
  2. <input> element of typeradio orcheckbox haschecked prop.

In contrast, the followings are uncontrolled elements with the initial values:

  1. A form element hasdefaultValue prop.
  2. <input> element of typeradio orcheckbox hasdefaultChecked prop.

Therefore, a form element should not have bothvalue anddefaultValue props. Also,<input> element should not have bothchecked anddefaultChecked props. React outputs a warning message if you set both props for a form element.

Noncompliant Code ExampleCompliant Code Example
1import React from 'react';1import React from 'react';
22
3export class FormExample extends React.Component {3export class FormExample extends React.Component {
4 constructor(props) {4 constructor(props) {
5 super(props);5 super(props);
6 this.state = {value: ''};6 this.state = {value: 'DeepScan'};
77
8 this.handleChange = this.handleChange.bind(this);8 this.handleChange = this.handleChange.bind(this);
9 this.handleSubmit = this.handleSubmit.bind(this);9 this.handleSubmit = this.handleSubmit.bind(this);
10 }10 }
1111
12 handleChange(event) {12 handleChange(event) {
13 this.setState({value: event.target.value});13 this.setState({value: event.target.value});
14 }14 }
1515
16 handleSubmit(event) {16 handleSubmit(event) {
17 alert('The submitted value: ' + this.state.value);17 alert('The submitted value: ' + this.state.value);
18 event.preventDefault();18 event.preventDefault();
19 }19 }
2020
21 render() {21 render() {
22 return (22 return (
23 <form onSubmit={this.handleSubmit}>23 <form onSubmit={this.handleSubmit}>
24 <label>24 <label>
25 Name:25 Name:
26 <input type="text" value={this.state.value} defaultValue="DeepScan" onChange={this.handleChange} /> {/* REACT_MISUSED_CONTROLLED_COMPONENT alarm because 'input' element should be either controlled or uncontrolled. */}26 <input type="text" value={this.state.value} onChange={this.handleChange} />
27 </label>27 </label>
28 <input type="submit" value="Submit" />28 <input type="submit" value="Submit" />
29 </form>29 </form>
30 );30 );
31 }31 }
32}32}

Noncompliant Code Example

View with compliant examples side by side
import React from 'react';export class FormExample extends React.Component {  constructor(props) {    super(props);    this.state = {value: ''};    this.handleChange = this.handleChange.bind(this);    this.handleSubmit = this.handleSubmit.bind(this);  }  handleChange(event) {    this.setState({value: event.target.value});  }  handleSubmit(event) {    alert('The submitted value: ' + this.state.value);    event.preventDefault();  }  render() {    return (      <form onSubmit={this.handleSubmit}>        <label>          Name:          <input type="text" value={this.state.value} defaultValue="DeepScan" onChange={this.handleChange} /> {/* REACT_MISUSED_CONTROLLED_COMPONENT alarm because 'input' element should be either controlled or uncontrolled. */}        </label>        <input type="submit" value="Submit" />      </form>    );  }}

Compliant Code Example

View with noncompliant examples side by side
import React from 'react';export class FormExample extends React.Component {  constructor(props) {    super(props);    this.state = {value: 'DeepScan'};    this.handleChange = this.handleChange.bind(this);    this.handleSubmit = this.handleSubmit.bind(this);  }  handleChange(event) {    this.setState({value: event.target.value});  }  handleSubmit(event) {    alert('The submitted value: ' + this.state.value);    event.preventDefault();  }  render() {    return (      <form onSubmit={this.handleSubmit}>        <label>          Name:          <input type="text" value={this.state.value} onChange={this.handleChange} />        </label>        <input type="submit" value="Submit" />      </form>    );  }}

Version

This rule was introduced in DeepScan 1.8.0-beta.

See

Was this documentation helpful?

Last updated on April 29, 2025

[8]ページ先頭

©2009-2025 Movatter.jp