gpt4 book ai didi

javascript - 组件正在将不受控制的文本类型输入更改为受控?

转载 作者:行者123 更新时间:2023-11-30 09:11:49 26 4
gpt4 key购买 nike

此时我尝试在表单中写入并将其保存在状态中,但出现此错误:

Warning: A component is changing an uncontrolled input of type text to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component.

import React from 'react';

class ExerciseNew extends React.Component {

constructor(props) {
super(props);
this.state = {value: ''}
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

handleSubmit = (e) => {
e.preventDefault();
console.log(this.state)
}

handleChange = (e) => {
this.setState({
[e.target.name]: e.target.value
})
}

render() {
return (
<div className="container">
<form
onSubmit={this.handleSubmit}
>
<div className="form-group">
<input
type="text"
className="form-control"
placeholder="title"
name="title"
onChange={this.handleChange}
value={this.state.title}
/>
</div>
<div className="form-group">
<input
type="text"
className="form-control"
placeholder="description"
name="description"
onChange={this.handleChange}
value={this.state.description}
/>
</div>
<div className="form-group">
<input
type="text"
className="form-control"
placeholder="img"
name="img"
onChange={this.handleChange}
value={this.state.img}
/>
</div>
<div className="form-row">
<div className="col">
<input
type="text"
className="form-control"
placeholder="leftColor"
name="leftColor"
onChange={this.handleChange}
value={this.state.leftColor}
/>
</div>
<div className="col">
<input
type="text"
className="form-control"
placeholder="rightColor"
name="rightColor"
onChange={this.handleChange}
value={this.state.rightColor}
/>
</div>
</div>
<button
type="submit"
className="btn btn-primary"
>
Submit
</button>
</form>
</div>
)
}
}

export default ExerciseNew;

我觉得很好奇,因为我正在关注 react 的文档, 以及 this video西类牙语。

我尝试使用 babeljs 和 ES7 的特性,这样就不必创建构造函数,所以我做了这样的事情:

import React from 'react';

class ExerciseNew extends React.Component {

state = {}

handleSubmit = (e) => {
e.preventDefault();
console.log(this.state)
}

handleChange = (e) => {
this.setState({
[e.target.name]: e.target.value
})
}

render() {
return (
<div className="container">
<form
onSubmit={this.handleSubmit}
>
<div className="form-group">
<input
type="text"
className="form-control"
placeholder="title"
name="title"
onChange={this.handleChange}
value={this.state.title}
/>
</div>
<div className="form-group">
<input
type="text"
className="form-control"
placeholder="description"
name="description"
onChange={this.handleChange}
value={this.state.description}
/>
</div>
<div className="form-group">
<input
type="text"
className="form-control"
placeholder="img"
name="img"
onChange={this.handleChange}
value={this.state.img}
/>
</div>
<div className="form-row">
<div className="col">
<input
type="text"
className="form-control"
placeholder="leftColor"
name="leftColor"
onChange={this.handleChange}
value={this.state.leftColor}
/>
</div>
<div className="col">
<input
type="text"
className="form-control"
placeholder="rightColor"
name="rightColor"
onChange={this.handleChange}
value={this.state.rightColor}
/>
</div>
</div>
<button
type="submit"
className="btn btn-primary"
>
Submit
</button>
</form>
</div>
)
}
}

export default ExerciseNew;

我仍然得到同样的错误。

最佳答案

您的表单已经是 controlled components .

你收到警告是因为你还没有初始化你的状态。您需要让每个变量都处于这样的状态,

this.state = {
title: '',
description: '',
img: '',
leftColor: '',
rightColor: ''
}

注意:如果您已经在为handleSubmithandleChange 使用箭头函数,则不需要在构造函数中绑定(bind)它们,

this.handleChange = this.handleChange.bind(this);  //not needed
this.handleSubmit = this.handleSubmit.bind(this); //not needed

实例:

class ExerciseNew extends React.Component {

constructor(props) {
super(props);
this.state = {
title: "",
description: "",
img: "",
leftColor: "",
rightColor: "",
};
}

handleSubmit = (e) => {
e.preventDefault();
console.log(this.state)
}

handleChange = (e) => {
this.setState({
[e.target.name]: e.target.value
})
}

render() {
return (
<div className="container">
<form
onSubmit={this.handleSubmit}
>
<div className="form-group">
<input
type="text"
className="form-control"
placeholder="title"
name="title"
onChange={this.handleChange}
value={this.state.title}
/>
</div>
<div className="form-group">
<input
type="text"
className="form-control"
placeholder="description"
name="description"
onChange={this.handleChange}
value={this.state.description}
/>
</div>
<div className="form-group">
<input
type="text"
className="form-control"
placeholder="img"
name="img"
onChange={this.handleChange}
value={this.state.img}
/>
</div>
<div className="form-row">
<div className="col">
<input
type="text"
className="form-control"
placeholder="leftColor"
name="leftColor"
onChange={this.handleChange}
value={this.state.leftColor}
/>
</div>
<div className="col">
<input
type="text"
className="form-control"
placeholder="rightColor"
name="rightColor"
onChange={this.handleChange}
value={this.state.rightColor}
/>
</div>
</div>
<button
type="submit"
className="btn btn-primary"
>
Submit
</button>
</form>
</div>
)
}
}

ReactDOM.render(
<ExerciseNew/>,
document.getElementById("root")
);
<div id="root"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.9.0/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.9.0/umd/react-dom.development.js"></script>

关于javascript - 组件正在将不受控制的文本类型输入更改为受控?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58157023/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com