From the following two questions, I understand that useRef
does not need to add update dependencies.
Related questions I looked at:
But when useRef
is used as props
, eslint
always warns about hook
missing dependencies?
import { useCallback, useEffect, useRef } from 'react';
const One = ({ refEl }) => {
const clickHandler = useCallback(e => {
refEl.current = 1111;
}, []); //⚠️ React Hook useCallback has a missing dependency: 'refEl'. Either include it or remove the dependency array.
useEffect(() => {
refEl.current = 2222;
}, []); //⚠️ React Hook useCallback has a missing dependency: 'refEl'. Either include it or remove the dependency array.
return (
<div onClick={clickHandler} ref={refEl}>
One
</div>
);
};
function App() {
const el = useRef('');
return <One refEl={el} />;
}
export default App;