Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
Why does the setState fn needs to be called through another function?

Lets look at the code below

import React from "react"

export default function App() {
    const [isGoingOut, setGoingOut] = React.useState(false);
    
    function yeet() {
        setGoingOut(prevValue => prevValue ? false : true);    
    }
    
    return (
        <div className="state">
            <h1 className="state--title">Do I feel like going out tonight?</h1>
            <div className="state--value" onClick={yeet}>
                <h1>{isGoingOut ? "Yes" : "No"}</h1>
            </div>
        </div>
    )
}

I'm calling the set function setGoingOut() through the function yeet(). However, if I directly call the setGoingOut() function as below

import React from "react"

export default function App() {
    const [isGoingOut, setGoingOut] = React.useState(false);
    
    setGoingOut(prevValue => prevValue ? false : true);
    
    return (
        <div className="state">
            <h1 className="state--title">Do I feel like going out tonight?</h1>
            <div className="state--value" onClick={setGoingOut}>
                <h1>{isGoingOut ? "Yes" : "No"}</h1>
            </div>
        </div>
    )
}

it throws an error. Is this related to the React philosophy of not letting the state be changed directly (the same reason we can't change a state variable count as count++ but have to do count + 1?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In the first code snippet, you are calling yeet which inside change isGoingOut using setGoingOut. This will surely works

function yeet() {
  setGoingOut((prevValue) => (prevValue ? false : true));
}

In the second snippet, you are changing state directly which will create infinite loop and produces error as:

Too many re-renders. React limits the number of renders to prevent an infinite loop.

You should change state not every time, but only when user clicks the button

setGoingOut(prevValue => prevValue ? false : true);

But, If you are trying to call directly setGoingOut then what it will do is:

  1. Pass event object to setGoingOut which will become value of isGoingOut and then you can't change its value because you are setting the state of isGoingOut as a new event object particularly SyntheticEvent that React send as an argument.

    onClick={setGoingOut}
    

This is not the way you should change state.

You can change state on click as:

<div
  className="state--value"
  onClick={() => setGoingOut((prevValue) => (prevValue ? false : true))}>
  <h1>{isGoingOut ? "Yes" : "No"}</h1>
</div>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!