useRef
- 应用场景:绑在DOM上,绑在组件上,保存临时变量永远不丢失
- 举个栗子
import React, { useState,useRef } from 'react'
export default function App() {
const [count, setCount] = useState(6)
var mycount = useRef(0); // 临时变量 mycount.current
return (
<div>
<button onClick={
()=> {
setCount(count + 1)
// mycount++;
mycount.current++
}
}>add</button>
{ count }-{ mycount.current }
</div>
)
}
标签:count,current,useRef,React,useState,mycount
From: https://www.cnblogs.com/bingquan1/p/16940277.html