react/prop-change-child.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};
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
console.log('handleClick\n');
this.setState((state, props) => ({
n: state.n + 1,
}));
}
render() {
console.log('NotMain.render');
return (
<>
<div onClick={this.handleClick}>Click me twice</div>
<div>NotMain this.props.n = {this.props.n} this.state.n = {this.state.n}</div>
<NotMain2 n={Math.floor(this.state.n / 2)} />
</>
);
}
}
class Main extends React.Component {
constructor(props) {
super(props);
console.log('Main.constructor');
this.state = {
n: 0,
};
}
render() {
console.log('Main.render');
return (
<div>
<div>Main this.state.n = {this.state.n}</div>
<NotMain n={this.state.n}></NotMain>
</div>
);
}
}
ReactDOM.render(
<Main/>,
document.getElementById('root')
);
</script>
</body>
</html>