I am trying to enter to values in a textbox and print them on button click by setting react state. I am pushing object in array initialized in a state the value of text boxes is pushing but not getting mapped. it giving error this.state.elements.map is not a function
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
this.state = {
elements: [
{element1 :"A",element2: "A"},
{element1 :"B",element2: "B"},
{element1 :"C",element2: "C"},
{element1 :"D",element2: "D"}
],
}
this.elementInput = "";
this.elementInput_2 = "";
}
addItems()
{
debugger
this.setState({
elements : this.state.elements.push({
element1 : this.elementInput.value ,
element2 : this.elementInput_2.value
})
})
console.log()
}
render() {
return (
<div>
<input type="text" placeholder="Enter Element" ref={(input) => this.elementInput = input} />
<input type="text" placeholder="Enter Element" ref={(input) => this.elementInput_2 = input} />
<button onClick={this.addItems.bind(this)}>Add</button>
{
this.state.elements.map((item,index)=> {
return <p key={index}>{item.element1 + " " + item.element2}</p>
})
}
</div>
);
}
}
export default App;