16
如何在React中强制使用挂钩重新渲染组件?
考虑下面的钩子示例 import { useState } from 'react'; function Example() { const [count, setCount] = useState(0); return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}> Click me </button> </div> ); } 基本上,我们使用this.forceUpdate()方法强制组件在React类组件中立即重新呈现,如以下示例所示 class Test extends Component{ constructor(props){ super(props); this.state = { count:0, count2: 100 } this.setCount = …