import React from 'react';
import reactDOM from 'react-dom';
class Count extends React.Component {
constructor() {
super();
}
state = {
count: 10
}
fn = () => {
console.log(this.btn2);
}
render() {
let { count } = this.state
return <div>
<h1>{count}</h1>
<button ref="btn" onClick={this.fn}>+</button>
<button ref={(el) => { this.btn2 = el }}>-</button>
</div>;
}
}
class App extends React.Component {
fn = () => {
console.log(this.Cn);
}
render() {
return <div>
<button onClick={this.fn}>按钮</button>
<Count ref={(el) => { this.Cn = el }} />
</div>;
}
}
reactDOM.render(<><Count /><App /></>, document.querySelector("#root"))