react/prop-change.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone@7.14.7/babel.min.js"></script>
</head>
<body>
<p><a href="https://cirosantilli.com/react">https://cirosantilli.com/react</a></p>
<div id="root"></div>
<script type="text/babel">
class NotMain2 extends React.Component {
constructor(props) {
super(props);
console.log('NotMain2.constructor');
this.state = {n: props.n};
}
render() {
console.log('NotMain2.render');
return (
<>
<div>NotMain2 this.props.n = {this.props.n}</div>
<div>NotMain2 this.state.n = {this.state.n}</div>
</>
);
}
}
class NotMain extends React.Component {
constructor(props) {
super(props);
console.log('NotMain.constructor');
this.state = { n: props.n };
}
render() {
console.log('NotMain.render');
return (
<>
<div>NotMain this.props.n = {this.props.n} this.state.n = {this.state.n}</div>
<NotMain2 n={this.props.n} />
</>
);
}
}
class Main extends React.Component {
constructor(props) {
super(props);
console.log('Main.constructor');
this.state = { n: 0 };
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
console.log('handleClick\n');
this.setState((state, props) => ({
n: state.n + 1,
}));
}
render() {
console.log('Main.render');
return (
<div>
<div onClick={this.handleClick}>Click me twice</div>
<div>Main this.state.n = {this.state.n}</div>
<NotMain n={Math.floor(this.state.n / 2)}></NotMain>
</div>
);
}
}
ReactDOM.render(
<Main/>,
document.getElementById('root')
);
</script>
</body>
</html>