What's wrong with the code below?
export default function App() {
const [count, setCount] = useState(0);
return (
<div className="App">
<h2>{count}</h2>
<button
onClick={() => {
setCount((count) => count + 1);
}}
>
increase
</button>
</div>
);
}
will using the arrow function in the event handler cause rerendering and affect performances?
Someone argued I should do this instead.
const [count, setCount] = useState(0);
const increment = () => setCount((count) => count + 1);
return (
<div className="App">
<h2>{count}</h2>
<button onClick={increment}>increase</button>
</div>
);
To me it's just a matter of preference, it doesn't improve performance, am I right?
https://codesandbox.io/s/purple-breeze-8xuxnp?file=/src/App.js:393-618
useCallbackbut no, given a single instance the two are the same — both redeclare a single function on each render.