当我按下按钮时,我想添加很多员工,但它只给我留下了一个。反应

When I press the button I want to add many Employees, but it only leaves me one. React

早上好,我有一个问题。当我按下 + 按钮时,只添加了一个员工行,我希望它在我按下时添加多少次

ReactJS 组件代码:

class Home extends React.Component {

    state = { showForm:false }

    showForm = () => {
        return(
            <Employee />
        )
    }

    render() {
        return (
            <div className='container-home'>
                <div className='min-margin'>
                    <Employee />
                    {this.state.showForm ? this.showForm() : null}
                    <div className='container-append'>
                        <button onClick={() => this.setState({showForm: true})}>➕</button>

                    </div>
                </div>
            </div>
        )
    }
}

您只需单击即可显示和隐藏输入。 您需要:

  1. 添加到状态数组:(输入:["Employee-0"])
  state = {
    showForm: false,
    inputs: ["Employee-0"]
  };
  1. 加入功能
  handleAddInput = e => {
    e.preventDefault();
    const inputState = this.state.inputs;
    let inputs = inputState.concat([`Employee-${inputState.length}`]);
    this.setState({
      inputs
    });
  };

  handleShowForm = e => {
    e.preventDefault();
    this.setState({
      ...this.state,
      showForm: !this.state.showForm
    })
  }
  1. 更改渲染中的代码
  render() {
    return (
      <div className="App">
        {this.state.showForm && <form>
          {this.state.inputs.map((input, idx) => (
            <Employee key={idx}/>
          ))}
        </form>}
        <button onClick={this.handleAddInput}>Add New Employee</button>
        <button onClick={this.handleShowForm}>Show form</button>
      </div>
    );
  }
  1. 点击按钮)

执行此操作存在不同的选项,但您所做的只是一个用于显示组件的标志。所以您可以尝试以下操作:

class Home extends React.Component {

state = { 
   employeesCount: 0,
   employees: []
}

render() {
    return (
        <div className='container-home'>
            <div className='min-margin'>
                {employees.map((eNumber) => {
                    return <Employee key={eNumber}/>
                }}
                <div className='container-append'>
                    <button onClick={() => this.setState({
                        employeesCount: employeesCount + 1,
                        employees: [...this.state.employess , (employeesCount + 1)]
                    })}>➕</button>
                </div>
            </div>
        </div>
    )
  }
}

试试这个:

import React from "react";

const Employee = (props) => {
  return(
    <div>Hello I am employee number {props.number}</div>
  )
}

class App extends React.Component {
  constructor() {
    super()
    this.state = { employees: [] }
  }


  addEmployee() {
    this.setState({
      employees: [...this.state.employees, <Employee number={this.state.employees.length} />]
    })
  }

  render() {
    return (
      <div>
        <div className='container-append'>
          <button onClick={() => this.addEmployee()}>➕</button>
        </div>
        { this.state.employees.map(employee => employee) }
      </div>
    )
  }
}
export default App;