I tried reading books and watching videos about JS and React, but still I don't have better understanding of React-Props & States. Can some one please explain me in details.
Asked
Active
Viewed 300 times
1
-
1Please check react documentation. – Pugazh Apr 26 '17 at 11:25
-
This is covered in the React docs. Is there a specific confusion? – Dave Newton Apr 26 '17 at 11:26
-
1Documents will surely help :). and this too https://github.com/uberVU/react-guide/blob/master/props-vs-state.md – Surya Purohit Apr 26 '17 at 11:27
-
https://stackoverflow.com/questions/27991366/what-is-the-difference-between-state-and-props-in-react – Ashish Kamble May 20 '19 at 06:00
1 Answers
5
Props Components Props can be thought of as a component's options. They're given as arguments to a component and look exactly like HTML attributes.
It's worth noting that a component should never change its props, they're immutable. If a component has data that's mutable, use the state object.
var Photo = React.createClass({
render: function() {
return (
<div className='photo'>
<img src={this.props.src} />
<span>{this.props.caption}</span>
</div>
);
}
});
React.render(<Photo src='http://example.com/lkevsb9' caption='Hong Kong!' />, document.body);
State The state object is internal to a component. It holds data which can change over time.
Before we can use state, we need to declare a default set of values for the initial state. This is done by defining a method called getInitialState() and returning an object.
State is set using the setState method. Calling setState triggers UI updates. Setting state should only be done from inside the component.
var InterfaceComponent = React.createClass({
getInitialState : function() {
return {
name : "chris"
};
},
handleClick : function() {
this.setState({
name : "bob"
});
},
render : function() {
return <div onClick={this.handleClick}>
hello {this.state.name}
</div>;
}
});
React.renderComponent(
<InterfaceComponent />,
document.body
);

Anup Singh
- 1,513
- 3
- 16
- 32