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

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

    Potential option is to force update only on specific component using key. Updating the key trigger a rendering of the component (which failed to update before)

    For example:

    const [tableKey, setTableKey] = useState(1);
    ...
    
    useEffect(() => {
        ...
        setTableKey(tableKey + 1);
    }, [tableData]);
    
    ...
    
    

提交回复
热议问题