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

303
Views
Why is the value on display not in sync with value logged to console?

Can anyone tell me why at button click, the value outputted to the console is always one unit smaller than displayed on the screen? The values are not in sync as expected.

Example below in React

In Child:

import React, {useState } from "react";

export const ChildComp = ({getNumProps}) => {
    const [num, setNum] = useState(0);
    

    const onPlusClick = () => {
        
        if (num< 12) {
          setNum(num + 1);// num does not increase immediately after this line, except when focus reenters here on second method call
        }
        
        getNumProps(num);
      }

    return(
        <div>
            <button onClick={onPlusClick}>
                Click to increment
            </button>
            {num}
        </div>
    );
}

In parent

import { ChildComp } from "./ChildComp"

export const ParentComp = () => {

    const getNum= (num) => {
        console.log(num);
    }

    return (<ChildComp getNumProps={getNum}/>)
}

The page initially shows 0

  • When I click once the number increments to 1, but console displays 0
  • When I click once the number increments to 2, but console displays 1 I should see in the console the same as the page display

Appreciate if you can leave a commen on how the question can be improved.

This is a child to parent communication example. Also, any objections about standards used, please let me know.

Thanks.

Update: I notice the values would be in sync if instead of getNumProps(num); I did getNumProps(num + 1); but that doesn't change the fact that previously on this line setNum(num + 1);, as already pointed out in the comment, num does not increase immediately after this line, except when focus reenters here on second method call. Not sure why.

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

0

The prop function getNumProps is a side effect, and should be put into a hook, instead of inside of that onPlusClick function.

Instead, do this:

useEffect(() => {
   getNumProp(num);
}, [num]);

Alternatively, to avoid the error: "React Hook useEffect has a missing dependency: 'getNumProps'. See this doc on using the useCallback hook

const callback = useCallback(() => {
   getNumProp(num);
}, [num]);

function onPlusClick(...) {
   ...
   callback();
}

The change to the state of num will cause a re-render of the child component, not the parent.

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!