How can I force component to re-render with hooks in React?

前端 未结 16 2225
误落风尘
误落风尘 2020-11-29 00:28

Considering below hooks example

   import { useState } from \'react\';

   function Example() {
       const [count, setCount] = useState(0);

       return         


        
16条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 01:07

    My variation of forceUpdate is not via a counter but rather via an object:

    // Emulates `forceUpdate()`
    const [unusedState, setUnusedState] = useState()
    const forceUpdate = useCallback(() => setUnusedState({}), [])
    

    Because {} !== {} every time.

提交回复
热议问题