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

237
Views
React state: unable to get updated value inside onChange

I am using an array of components that are interested depending on various conditions i.e the order and number of elements in the array are dynamic as shown below:

  useEffect(() => {
    const comp = [];

    // if(condition1===true){
    comp.push(<MyComp onChange={onValueChange} />);
    // }
    // if(condition2===true){
    comp.push(<YourComp onChange={onValueChange} />);
    // }
    // if(condition3===true){
    comp.push(<HisComp onChange={onValueChange} />);
    // }
    setComponents(comp);
  }, []);

To each of the components in the array, there could be some sort of input control like input-text, input-number, text-area, chips, radio, checkbox, etc.
So there is an onChange event linked to each of these components.

I am using a common onValueChange function which is passed as a callback to these components. In the onValueChange I need 2 things:

  • changed value (from child component)
  • activeIndex (from same component)
  const onValueChange = (val) => {
    console.log("onChange Valled", val, "activeIndex==", activeIndex);
  };

But here I am not able to fetch the updated value on activeIndex, it always gives zero no matter in what active step I am in.

Sandbox DEMO

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Try using useCallback with dependency array. Also try to avoid storing components in state - the office advice - what shouldn’t go in state?

const onValueChange = useCallback((val) => {
    console.log("onChange Valled", val, "activeIndex==", activeIndex);
  },[activeIndex];

For rendering try something like below.

condition1===true && <MyComp onChange={onValueChange} />

or create a function which returns the component eg: getComponent(condition) and use this in render/return. Make sure you wrap getComponent in useCallback with empty dependency array []

about 4 years ago · Santiago Trujillo Report

0

 useEffect(() => {

    setComponents((previousValues)=>{
        // if you want to add on previous state
        const comp = [...previousValues];

        // if you want to overwrite previous state
        const comp = [];

        if(condition1===true){
          comp.push();
        }
        if(condition2===true){
         comp.push();
        }
        if(condition3===true){
          comp.push();
        }
      return comp;
    });
  }, []);
about 4 years ago · Santiago Trujillo 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!