gpt4 book ai didi

javascript - 添加到数组中

转载 作者:行者123 更新时间:2023-12-01 02:01:14 25 4
gpt4 key购买 nike

想要添加电影并将其推送到 Movie 类内部的数组中。当我运行它时,我收到此警告:

index.js:2178 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. More info: https://reactjs.org/docs/forms.html#controlled-componentsin input (at index.js:41)

in label (at index.js:39)

in form (at index.js:38)

in Movie (at index.js:50)

class Movie extends React.Component {
constructor(props) {
super(props);
this.state = {value: ''};
this.state = {list: []};//this line shows a waring

this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.addMovie = this.addMovie.bind(this);

}

handleChange(event) {
this.setState({value: event.target.value});
}

handleSubmit(event) {
alert('A movie was submitted: ' + this.state.value);
event.preventDefault();
this.addMovie();

}

addMovie(value){
this.setState({ list: [...this.state.list, value] });
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Movie name:
<input type="text" value={this.state.value} onChange={this.handleChange} />
</label>
<input type="submit" value="Submit" />
</form>
);
}
}

ReactDOM.render(
<Movie />,
document.getElementById('root')
);

最佳答案

构造函数中不能有多个 this.state

  constructor(props) {
super(props);
this.state = {value: '',list: []};

this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.addMovie = this.addMovie.bind(this);

}

class App extends React.Component {
constructor(props) {
super(props);
this.state = { value: '', list: [] };


this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.addMovie = this.addMovie.bind(this);

}

handleChange(event) {
this.setState({ value: event.target.value });
}

handleSubmit(event) {
//alert('A movie was submitted: ' + this.state.value);
event.preventDefault();
this.addMovie();

}

addMovie() {
let value = this.state.value;
this.setState({ list: [...this.state.list, value],value: '' });
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>

Movie name:
<ul>
{this.state.list !== [] && this.state.list.map((row, i) => <li key={i} >{row}</li>)}
</ul>
<input type="text" value={this.state.value} onChange={this.handleChange} />
</label>
<input type="submit" value="Submit" />
</form>
);
}
}


ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='root'></div>

关于javascript - 添加到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50558137/

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