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

206
Views
React Same Component Communication

Good Morning,

I'm creating an react app, this application will have four "sides" and each side can have different values so I mapped the same component 4 times:

:[...Array(item)].map((data) =>
          <Side 
           key={ data }
           Style='wall-holder' 
           InputClass='input-height' 
           />
      )
 }

So each Side could work as its own thing, in each side the user can input an specific value using the inputs that exists in the Side component:

Side Component= input= 1, input =2;
Side Component= input=3, input =5;
Side Component= input=3, input =5;
Side Component= input=3, input =5;

note: is only one component Side, I just mapped it 4 time so i could reuse it

Now I want to be able to get all of those inputs in an array or object so I can add all of the input values, however when I try to send it to a global state with the api context, it only keeps the last added value, I understand why is this happening(kinda), but I dont know yet how to solve this problem

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

0

Keep a state in your parent component like below. use index to update each input state of Side components.

import {useState} from "react";

const ParentComp = () => {
    const [state, setState] = useState({});
    
    return [...Array(item)].map((data, dataIndex) =>
          <Side 
           key={ data }
           Style='wall-holder' 
           InputClass='input-height' 
           setState={setState}
           dataIndex={dataIndex}
           />
      )
 }

In your Side component.

<input
    onChange={e =>
        props.setState(prevState => {
            return { ...prevState, [props.dataIndex]: e.target.value };
        })
    }
/>;
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!