State Example in ReactJs

State  in reactjs belong to single component . it is not passed in between component as props works . Component state can be modified over time in response to user actions, network responses, and anything. Here we are given example how to we change state of component dynamically .

App.js

import React, { Component } from ‘react’;
import ‘./App.css’;
import User from ‘./User/User’;
import Profile from ‘./Profile/Profile’;
class App extends Component {

state= {
users :[
{ name : ‘ram’ , salary :2000 },
{ name : ‘john’ , salary :3000}
]
}
render() {
return (
<divclassName=”App”>
<h1> My React Application </h1>
<p>user name is {this.state.users[0].name} and salary is {this.state.users[0].salary}</p>
<p> user name is {this.state.users[1].name} and salary is {this.state.users[1].salary}</p>
</div>
);
}
}
export default App;
In this example we app.js file and it have state arary users. This array can be update on user action . We can access state values inside render function with the help of {this.state.statename.filed}

Leave a Reply

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

− 3 = 2