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

102
Views
React can the callback function of useState increase performance?

With Callback

  const [state, setState] = useState(() => {
    email: '',
    password: '',
  });

  const handleStateChange = useCallback((stateName) => (e) => {
    setState((prevState) => ({
      ...prevState,
      [stateName]: e.target.value,
    }));
  }, []);

Without Callback

const [state, setState] = useState(() => {
    email: '',
    password: '',
  });

  const handleStateChange = useCallback((stateName) => (e) => {
    setState({
      ...state,
      [stateName]: e.target.value,
    });
  }, [state]);

It's possible to use dispatch of useState without appending dependencies through the callback function.

I think it reduces dependencies so that helps for performance.

Is this correct?

In addition, sometimes, the callback argument doesn't get current state correctly. I don't know why it doesn't work.

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

0

There likely isn't a measurable difference between the two approaches (but you could try testing it!). This is extreme early optimization, and if you want to increase performance, you should focus on other parts of your app that can yield better results.

The difference between the two approaches is to solve different problems (getting the prevState vs not getting It). It calls the same logic internally, and React should be smart enough to not create a performance difference between the two.

Think of it this way: do you worry about adding an extra state or two because of performance? No, of course not, because it doesn't matter.

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!