类组件
import React,{Component, Fragment, useState} from "react"
class App extends Component{
state={
num:1
}
onClick=()=>{
this.setState({
num:this.state.number+1
})
}
render(){
return (
<>
<h1>{this.state.number}</h1>
<button onClick={this.onClick.bind(this)}></button>
</>
)
}
}
export default App
函数组件
import React,{Component, Fragment, useState} from "react"
function App(){
const [num,setNum]=useState(1)
const addNum=()=>{
setNum((num)=>num+1)
}
return(
<>
<h1>数字为:{this.state.num}</h1>
<button onClick={{addNum}}>累加</button>
</>
)
}
export default App
useRef获取到子组件