New in reactJS, i am trying to adding an item in array by clicking on button . but could not fetch changed array

Amit Rullez

I have an array in which i have 4 value , then i have a button on click i am pushing new value to the array, so initially i am showing item list of array data and then click on button i am pushing new value to array but could not update the item list . here is my code .

import React, {Component} from 'react';
import './Amit.css'; 

class Item extends Component {
  state = {
   item : [
    {
     name:'Item 1'
    },
    {
     name:'Item 2'
    },
    {
     name:'Item 3'
    },
    {
     name:'Item 4'
    }
  ]
 }

 addItem =() => {
   let item = [...this.state.item];
   item.push("New Item");
   this.setState ({ item: item });
   alert(item);
 }

 render(){
    return(
     <div className="Item-block">
        <div className="item-heading">
             <h4 className="item-heading-text"> ITEM HEADING  </h4>
        </div>
        <div className="item-add">
            <button onClick={this.addItem}> ADD NEW ITEM </button>
        </div>
        <div className="item-list">
            <ul>
                {
                  this.state.item.map((item)=> {
                    return <li name={item.name}> {item.name} </li>
                  })
                }
            </ul>
        </div>
      </div>

    );
  }

}

export default Item;
Philippe

This is how I would write it:

class Item extends Component {

  constructor (props) {

    super(props)  

    this.addItem = this.addItem.bind(this)

    this.state = {
        items : [{
          name: 'Item 1'
        },{
          name:'Item 2'
        },{
          name:'Item 3'
        },{
          name:'Item 4'
        }]
      }
  }

  addItem() {

    const items = [
      ...this.state.items, {
        name: "New Item"
      }
    ]

    this.setState ({ 
      items 
    })
  }

  renderItems() {

    const items = this.state.items.map((item, idx)=> {
      return (
        <li key={item.name + idx} name={item.name}> 
          {item.name} 
        </li>
      )
    })

    return items
  }

  render () {
    return(
        <div className="Item-block">
            <div className="item-heading">
                <h4 className="item-heading-text"> 
                  ITEM HEADING 
                </h4>
            </div>
            <div className="item-add">
                <button onClick={this.addItem}> 
                  ADD NEW ITEM 
                </button>
            </div>
            <div className="item-list">
                <ul>
                    { this.renderItems ()}
                </ul>
            </div>
        </div>
      );
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MongoDB > Adding a new item into an object inside an array

From Dev

Java/Android: adding/removing values to/from array by clicking a button?

From Dev

I am trying to load variables in an array

From Dev

I am trying to copy an variables into an array

From Dev

i am trying to make a array of a object in php

From Dev

I am trying to copy an variables into an array

From Dev

Adding Array to MySQL Fetch

From Dev

I am new in using ArrayLists. Array Out of Bounds, Array seems to be empty, but have been adding Strings to it already

From Dev

I am trying to copy the data of one array to another array

From Dev

Adding new rows to table on clicking button in JQuery

From Dev

Adding new <select> tag by clicking on the button

From Dev

I am trying to stop my function from displaying the same object twice when clicking a button

From Dev

Reference is adding item to array

From Dev

Reference is adding item to array

From Dev

Adding item on array in javascript

From Dev

Time complexity of adding a new item to the back of a array-based list

From Dev

Trying to understand 'mysqli_fetch_array' I fail to run an example

From Dev

ReactJS fetch for each value of array

From Dev

As soon as i add an item to my array i am unable to delete an item from my array

From Dev

I am trying to convert a string of words into an array with the spaces included

From Dev

I am getting undefined while trying to pass array to a class

From Dev

Vuejs, I am trying to do a push of a subobject to an array

From Dev

I could not push an new data into array when use upsert

From Dev

Adding item to DataGridView by clicking button using c#

From Dev

function adding space to array item

From Dev

function adding space to array item

From Dev

Adding JSON array to DynamoDB Item

From Java

(Angular) Clicking on a button highlights another button because I'm changing a value in an array. Why?

From Dev

How can I delete an item from array with a button in SwiftUI?

Related Related

  1. 1

    MongoDB > Adding a new item into an object inside an array

  2. 2

    Java/Android: adding/removing values to/from array by clicking a button?

  3. 3

    I am trying to load variables in an array

  4. 4

    I am trying to copy an variables into an array

  5. 5

    i am trying to make a array of a object in php

  6. 6

    I am trying to copy an variables into an array

  7. 7

    Adding Array to MySQL Fetch

  8. 8

    I am new in using ArrayLists. Array Out of Bounds, Array seems to be empty, but have been adding Strings to it already

  9. 9

    I am trying to copy the data of one array to another array

  10. 10

    Adding new rows to table on clicking button in JQuery

  11. 11

    Adding new <select> tag by clicking on the button

  12. 12

    I am trying to stop my function from displaying the same object twice when clicking a button

  13. 13

    Reference is adding item to array

  14. 14

    Reference is adding item to array

  15. 15

    Adding item on array in javascript

  16. 16

    Time complexity of adding a new item to the back of a array-based list

  17. 17

    Trying to understand 'mysqli_fetch_array' I fail to run an example

  18. 18

    ReactJS fetch for each value of array

  19. 19

    As soon as i add an item to my array i am unable to delete an item from my array

  20. 20

    I am trying to convert a string of words into an array with the spaces included

  21. 21

    I am getting undefined while trying to pass array to a class

  22. 22

    Vuejs, I am trying to do a push of a subobject to an array

  23. 23

    I could not push an new data into array when use upsert

  24. 24

    Adding item to DataGridView by clicking button using c#

  25. 25

    function adding space to array item

  26. 26

    function adding space to array item

  27. 27

    Adding JSON array to DynamoDB Item

  28. 28

    (Angular) Clicking on a button highlights another button because I'm changing a value in an array. Why?

  29. 29

    How can I delete an item from array with a button in SwiftUI?

HotTag

Archive