States and Props in ReactJS

In this article we are reading about States and Props in ReactJS”.
Dynamic apps must need to pass data around it’s system. In React, data movement happens mostly among components and external services that provide the original data (eg HTTP, localStorage).
Props are immutable and dumb which means they can only be passed from parent components down and cannot be changed. This poses a challenge because, modern apps do not rely on having all of it’s states ready on page load. Ajax or Events could happen and when data returns, someone needs to be responsible for making updates. This is where React states comes in.
On initializing React, we define an initial state and keep the state in sync with the props. Once the state is updated, the props can then be easily kept in sync:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//js
var Counter = React.createClass({
getInitialState: function() {
return {
counter: 0
};
},
render: function() {
return
<div>
<h2>{this.props.name}</h2>
{this.state.counter}

</div>
;
}
});

ReactDOM.render(
,
document.getElementById('container')
);
1
2
3
//html
<script src="https://facebook.github.io/react/js/jsfiddle-integration-babel.js"></script>
<div id="container"><!-- This element's contents will be replaced with your component. --></div>
The example is still dumb because the responsibility of state in the context can still be achieved with props. While we go deeper, we will see better reasons for the existence of states.
So in this article we have read about “States and Props in ReactJS”.

Comments

Popular posts from this blog

Java Number Class

Java Character Class

What are Web Resources in Microsoft Dynamics 365?