首发:CSDN碰磕,分享自己的学习日志
天晴
✊✊阳光明媚Study…
2022/6/14
相当于useState一样也可以进行保存引用值
- useRef(‘默认值’)
通过.current得到它
//基本用法绑定ref
const myinput=useRef(null);
<input ref={myinput} />
/**
* useRef(保存引用值)
*/
import React, { useState, useRef } from 'react';
export default function MyRefs() {
const [count, setcount] = useState(0)
var mycount=useRef(0)
return (
<div>
<button onClick={() =>{
setcount(count+1)
mycount.current+=1
}}>+</button>
{count}-{mycount.current}
</div>
);
}