0

I have a question regarding an anonymous function calling setState. I am trying to do a basic exercise (playground) where the number displayed on the button is increased when you click on it.

function Button() { const [state, setState] = useState(1); return <button onClick={() => setState(state+1)} >{state}</button>; } ReactDOM.render( <Button />, document.getElementById('mountNode'), ); 

I am confused why calling setState directly (instead of calling the anonymous function that calls the setState) does not work.

return <button onClick={setState(state+1)} >{state}</button>; 

5 Answers 5

3

onClick expects a callback function, i.e it expects a function declaration, which will be executed on click

The difference between onClick={()=> setState()} vs onClick={setState()} is that with the first one you're passing a function that, when executed will call setState but, with second you're passing what would be returned by calling setState

So, with onClick={setState()}, setState gets called immediately, which tells react to update the component, so react calls the render method again, and setState gets called again, which again tells react to update the component and so on... And react ends up in an infinite loop.

Sign up to request clarification or add additional context in comments.

Comments

0

On every render in this code return <button onClick={setState(state+1)} >{state}</button>; setState(state+1) will get called. This will cause react to crash.

You need to pass onClick a callback function () => setState(state + 1) so that the function is not called on every render.

Comments

0

return <button onClick={setState(state+1)} >{state}</button>;

You aren't setting onClick to call setState here, you're setting onClick to the return value of the function call on setState (you are actually invoking the function, not assigning it). This is almost certainly not what you are wanting, and will result in React crashing due to the render process starting a state update which will result it in needing to rerender, causing a cycle of rerenders.

The anonymous function works because in that case you are assigning a function which will call setState, not the return value of setState.

Comments

0

you should use it like this.

return <button onClick={setState(state+1)} >{state}</button>; 

1 Comment

Please don't post only code as an answer, but also provide an explanation of what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes
0

you can replace your code with this by adding React.StrictMode

import React, {useState } from 'react'; import ReactDOM from "react-dom"; export default function Button() { const [state, setState] = useState(1); return ( <div> <p>Vous avez cliqué {state} fois</p> <button onClick={() => setState(state + 1)}> {state} </button> </div> ); } const rootElement = document.getElementById("root"); ReactDOM.render( <React.StrictMode> <Button /> </React.StrictMode>, rootElement );

1 Comment

Welcome to StackOverflow. Adding an explanation to your solution would help everyone to understand it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.