问题
I'm using useState() in function component and first render is calling twice. Is it correct or mistake? If it is correct why does it render twice? setCount renders the component twice too.
function Example() {
const [count, setCount] = useState(0);
console.log("render");
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
ReactDOM.render(<Example />, document.getElementById('uu5'));
Thanks
回答1:
Problem is in React DevTools. When console is closed, component is rendered just one times. But if you open React DevTools and reload the page, render will be shown two times. Open example and try it. (React 16.8.3)
来源:https://stackoverflow.com/questions/54927622/usestate-do-double-render