Hello friends, welcome again on my blog. Today in this blog post, I am going to tell you, How to Push new values to reactjs array state?
For reactjs new comers, please check the below link:
Friends here is the working code snippet for “How to Push new values to reactjs array state?” and please use this carefully to avoid mistakes:
1. Guys, here is the code and I have added into my App.js file and you can add this whereever you want:
import React from 'react';
import './App.css';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
cars:['Range Rover', 'BMW', 'Mercedes-Benz']
}
}
componentDidMount() {
//Pushing New Values
let arr = this.state.cars;
arr.push('G Wagon', 'Jaguar', 'Skoda');
this.setState({cars: arr});
}
render() {
return (
<div className="maincontainer">
<h1> ReactJs</h1>
<div>
<ul>
{this.state.cars.map((car) => {
return (
<li>
{car}
</li>
)
})}
</ul>
</div>
</div>
)
};
}
export default App;
Now we are done friends. If you have any kind of query or suggestion or any requirement then feel free to comment below.
Note: Friends, I just tell the basic setup and things, you can change the code according to your requirements.
I will appreciate that if you will tell your views for this post.Nothing matters if your views will good or bad.
Jassa
Thanks
