How to Push new values to reactjs array state?

·

How to Push new values to reactjs array state?

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:

Reactjs Basic Tutorials


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

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.