Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

reactjs - add dynamic empty objects to an array React

I have the following code, which basically adds empty objects to an array.

  handleAddNewRow = () => {
    this.setState({
      rowData: [
        { MEMBER: "", ALIAS: "", STATUS: "" },
        ...this.state.rowData
      ]
    })
}

Lets say, I am passing an integer value to the function handleAddNewRow and then it dynamically adds the number of empty objects to the array based on the integer value, How is it possible?

question from:https://stackoverflow.com/questions/65838426/add-dynamic-empty-objects-to-an-array-react

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can look at my function:

handleAddNewRow = (number) => {
   this.setState({
       rowData: [
            ...this.state.rowData,
            ...(new Array(number).fill({ MEMBER: "", ALIAS: "", STATUS: "" }))
       ]
   });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...