1

I have the following Array

arrayOfItems: [{
0:  
    description: "item1"
    id: 11
    name: "item1Name"   
},
1:  
    description: "item2"
    id: 12
    name: "item2Name"   
},
2:  
    description: "item3"
    id: 13
    name: "item3Name"   
},
3:  
    description: "item4"
    id: 14
    name: "item4Name"   
}]

I want to add a new pair

{   
    description: "item5"
    id: 15
    name: "item5Name"   
}   

I am still very new to React and have been working on this problem. I do understand how Map works but not sure how I can add new pair in React

This component is a dropdown list so there is no input or button click related to it.

{dataArray.arrayOfItems!.map((item: any) => {
return (
  <ComponentName key={item.id} value={item.description}>
    {item.description}
  </ComponentName>
);
})}
4
  • set array in state , on button click event , get value of input and push to array Commented Jun 14, 2019 at 15:16
  • @SaurabhMistry, there is not button click. This is a dropdown list. Sorry I should have mentioned that Commented Jun 14, 2019 at 15:17
  • ok , when should an item add to list ? Commented Jun 14, 2019 at 15:18
  • @SaurabhMistry when the page loads. Commented Jun 14, 2019 at 15:18

4 Answers 4

2

if you want to add item to array on page load use componentDidMount() method:

class List extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      items:[
       {id:1,name:'aaa', description:'this is description aaa'},
       {id:2,name:'bbb', description:'this is description bbb'},
      ]
    }
  }

  componentDidMount(){
       let items=this.state.items;
       let newItem={id:5,name:'ccc',description:'this is description ccc'};
       let updatedItems=items.push(newItem);
        // or you can use ... spread operator
       // let updatedItems=[...items,newItem];
       this.setState({items:updatedItems});
  }


}
Sign up to request clarification or add additional context in comments.

Comments

1

You can store your array into state, and then modify the state.

Here's an example

function MyComponent() {
  const [items, setItems] = React.useState([{ id: 0, description: 'Old Item' }])

  const loadMoreItems = () => {
    setItems([...items, { id: 1, description: 'New Item' }])
  }

  return (
    <>
      {items.map((item) => (
        <div key={item.id} value={item.description}>
          <p>{item.description}</p>
        </div>
      ))}
      <button onClick={loadMoreItems}>Load more items</button>
    </>
  )
}

Comments

0

Add on change event to your dropdown.

onChange = (event) => {
  console.log(event.target.value)
  // add your value to array here
  this.setState((prevState) => {
    arrayOfItems: [...prevState.arrayOfItems, yourItem],
  })
}

<select onChange={this.onChange}>
</select>

EDIT

Adding values on page load. Don't use push to add items to array in state.

componentDidMount = () => {
  this.setState((prevState) => {
    arrayOfItems: [...prevState.arrayOfItems, yourItem],
  })
}

1 Comment

never use push to change array in state, daveceddia.com/why-not-modify-react-state-directly
0
let fileInfos=this.state.fileInfos;

fileInfos.push({
                  "name": file.name,
                  "content": e.target.result
                  });

    this.setState({fileInfos});

1 Comment

this is equivalent to the spread operator

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.