📜  react 中的动态表单 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:05.857000             🧑  作者: Mango

代码示例1
class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { values: [] };
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  createUI(){
     return this.state.values.map((el, i) => 
         
) } handleChange(i, event) { let values = [...this.state.values]; values[i] = event.target.value; this.setState({ values }); } addClick(){ this.setState(prevState => ({ values: [...prevState.values, '']})) } removeClick(i){ let values = [...this.state.values]; values.splice(i,1); this.setState({ values }); } handleSubmit(event) { alert('A name was submitted: ' + this.state.values.join(', ')); event.preventDefault(); } render() { return (
{this.createUI()}
); } } ReactDOM.render(, document.getElementById('container'));