1,保存变量永远不丢失
import React, { useState,useRef } from 'react'
export default function App() {
const [count,setcount] = useState(0)
var mycount = useRef(0)//保存变量永远不丢失--useRef用的是闭包原理
return(
<div>
<button onClick={()=>{
setcount(count + 1)
mycount.current++
}}>add</button>
{count} - {mycount.current}
</div>
)
}
2 绑定在dom上或组件上,无论实现dom原生节点访问还是组件通信都可以