I have a function component, and I want to force it to re-render.
How can I do so?
Since there\'s no instance this, I cannot call this.forceU
The accepted answer is good. Just to make it easier to understand.
Example component:
export default function MyComponent(props) {
const [updateView, setUpdateView] = useState(0);
return (
<>
<span style={{ display: "none" }}>{updateView}</span>
</>
);
}
To force re-rendering call the code below:
setUpdateView((updateView) => ++updateView);