Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we pass null as first value in useRef, React JS?

I am little bit confuse about it! Is there any difference if we pass or don't pass null as an initial parameter in useRef? Like when we create state in functional component of React Js, we pass null as first value and then we update it! What if we also pass null in useRef like

const ref = useRef();

Or

const ref = useRef(null);

Both are correct?

like image 492
Mohammad Fareed Alam Avatar asked Apr 02 '26 15:04

Mohammad Fareed Alam


1 Answers

Both are correct but have different meanings.

const ref = useRef();

is (since unpassed parameters are undefined) the same as

const ref = useRef(undefined);

so the initial value of the ref is undefined, while with

const ref = useRef(null);

the initial value is null.

like image 182
AKX Avatar answered Apr 04 '26 06:04

AKX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!