import styles from './index.less';
import React, { Component, createContext } from 'react'
const BatteryContext = createContext(0);
class Leaf extends Component {
static contextType = BatteryContext
render() {
const battery = this.context
return (
<h1 className={styles.title}>Battery:{battery}</h1>
)
}
}
class Middle extends Component {
render() {
return <Leaf />
}
}
export default class IndexPage extends Component {
state = {
battery: 60,
};
render() {
const { battery } = this.state;
return (
<div>
<BatteryContext.Provider value={battery}>
<button type="button" onClick={() => this.setState({ battery: battery - 1 })}>Press</button>
<Middle />
</BatteryContext.Provider>
</div>
);
}
}