Categories

Friday, April 19, 2024
#919814419350 therichposts@gmail.com
Reactjs

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

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

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