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

前端 未结 16 2215
误落风尘
误落风尘 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:11

    One line solution:

    const useForceUpdate = () => useState()[1];

    useState returns a pair of values: the current state and a function that updates it - state and setter, here we are using only the setter in order to force re-render.

提交回复
热议问题