"Too many re-renders. React limits the number of renders to prevent an infinite loop" error when useState hook is used

M S
import {useState} from 'react';

const Counter = () =>{

    let [counter,setCounter] = useState(0);

    return(
       <div className='counter-box'>
            <span>{counter}</span>
            <button onClick={setCounter(counter++)}></button>
       </div>
    )
}
export default Counter;

I'm using functional component here. Can someone tell me what's wrong with my code?

nightElf

Yes. Here the onClick function is getting executed when it's rendered instead of user click event.

Solution 1:

import {useState} from 'react';

const Counter = () =>{

    let [counter,setCounter] = useState(0);

    return(
       <div className='counter-box'>
            <span>{counter}</span>
            <button onClick={e => setCounter(counter+1)}></button>
       </div>
    )
}
export default Counter;

Instead of executing the code, we pass a function reference(anonymous arrow function) to the onClick event.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why it is not showing error, when using const in useState hook

Type Problem: How can useState(react-hook) be used as a global variable when contextAPI is used in typescript?

Invalid Hook Call Error when using react useState hook inside firebase app

Why useState is throwing error when used in main App functional component?

React setState from useState hook causes exception when used in callback passed to RxJS subsciption

While using useState hook and onClick event in react I get error when calling the function in onClick event

NaN error in React when I'm trying to use the useState hook in different components?

Why react hook usestate show an error message

Error: React Hook "useState" is called in function

React hook logging a useState element as null when it is not

react.js useState hook is assigned a value but not used, but that is ok

React - useState not updating when used inside of useEffect

override useState variable when it is used in useEffect

React useState hook - when to use previous state when updating state?

Why does react hook throw the act error when used with fetch api?

React hook: used in render method (Error: Invalid hook call)

What is the solution to the error 'React Hook "useState" cannot be called in a class component'

React Invalid Hook Call Error with useState In Functional Component

Set state when testing functional component with useState() hook

useState hook setter is running only once when called by an EventListener

React Native State Management Question - when does useState hook load?

How to declare array as any: when using UseState hook?

How to refactor an if else if with previous state when using useState Hook?

Component not re-rendering when useState hook updates

RadioGroup will not select when onChange is set that uses useState hook

React - infinite loop using get request when using useState hook

useState() hook removes fields from the object when setting new value

Hook useState logs state when it should not. Example codebox included

React component rendering twice when using useState hook