import React,{useRef, useState} from 'react'
/**
*
* @returns useRef创建的对象存在于整个生命周期中,每次返回的都是都一个对象
*/
export default function Test() {
const [count,setCount] = useState(0);
const uRef = useRef(0);
const reactRef = React.createRef(0);
if(!uRef.current){
uRef.current = count;
}
if(!reactRef.current){
reactRef.current = count;
}
console.log(uRef,reactRef);
return (
<div>
<p>createRef:{reactRef?.current}</p>
<p>useRef:{uRef?.current}</p>
<strong>createRef</strong>
<strong>useRef</strong>
<button onClick={()=>{setCount(prev=>++prev)}}>count++</button>
</div>
)
}
useRef创建的对象存在react整个生命周期当中
最新推荐文章于 2023-12-22 10:42:02 发布