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

239
Views
Pass props and use ID again

I have a component that passes props to another component. Inside the component the props have been passed to, I declare the parameter set new variable and get the last item of the array like this:

var lastItem = passedProp[passedProp - 1] || null

My question is how do I pass this property back to another component to use in a global service I am using to run inside a function. From what I am aware props can only be passed down in React, not up? Please correct me if I am wrong. The end result I want to achieve is to use this property's ID in function I am using in global service.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

read about lifting state up ... https://reactjs.org/tutorial/tutorial.html#lifting-state-up

about 4 years ago · Santiago Trujillo Report

0

You can pass a function to the child and the child can pass the information through this function.

I let you an example that you can copy & paste to see how it works :)

import React  from 'react';

function ChildComponent(props) {
  const { data, passElementFromChild } = props;
  const lastElement = data[data.length - 1] || null;

  setTimeout(() => {
    passElementFromChild('this string is what the parent is gonna get');
  }, 300);

  return (
    <div>Last element of the array is: {lastElement}</div>
  );
}

function Question17() {

  const data = ['firstElement', 'middleElement', 'lastElement']

  const passElementFromChild = (infoFromChild) => {
    console.log("infoFromChild: ", infoFromChild);
  }

  return (
    <ChildComponent data={data} passElementFromChild={passElementFromChild} />
  );
}

export default Question17;
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!