state and props in react js code example
Example 1: reading state react
class Example extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
render() {
return (
<div>
<p>You clicked {this.state.count} times</p>
<button onClick={() => this.setState({ count: this.state.count + 1 })}>
Click me
</button>
</div>
);
}
}
Example 2: state and props
What is the Props
Props is short for properties and they are used to pass data between React components. React’s data flow between components is uni-directional
wt is State
which allows components to create and manage their own data
Example 3: reading state react
<p>You clicked {count} times</p>
Example 4: props and state react
function tick() {
const element = (
<div>
<h1>Hello, world!</h1>
<h2>It is {new Date().toLocaleTimeString()}.</h2>
</div>
);
ReactDOM.render( element, document.getElementById('root') );}
setInterval(tick, 1000);