企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
~~~ import React, { Component } from "react"; class App extends Component { constructor(props) { super(props); this.state = { value: "hello world", list: [] } } render() { return ( <div> <input value={this.state.value} onChange={this.handleChange.bind(this)} /> <button onClick={this.handleClick.bind(this)}>增加</button> <ul> {this.state.list.map((item, index) => { return <li key={index}>{item}</li> })} </ul> </div> ) } handleChange(e) { this.setState({ value: e.target.value }) } handleClick() { var list = this.state.list; //不要直接修改state if (!list.includes(this.state.value)) { list.push(this.state.value); } this.setState({ list, value:"" }) } } export default App; ~~~