React Hooks: Why is .current null for useRef Hook?

后端 未结 3 550
温柔的废话
温柔的废话 2020-12-24 11:41

I have a simple example of a component:

function App() {
  const observed = useRef(null);
  console.log(observed.current);

  return (
    
3条回答
  •  时光说笑
    2020-12-24 11:59

    This is what I ended up doing since calling useEffect on rlvRef did not capture all events.

    const rlvRef = useRef()
    const [refVisible, setRefVisible] = useState(false)
    
    useEffect(() => {
      if (!refVisible) { 
        return
      }
      // detected rendering
    }, refVisible)
    
    return (
     { rlvRef.current = el; setRefVisible(!!el); }}
    ... />
    )
    

提交回复
热议问题