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

181
Views
Get Parameter Data from Child then Pass to the Parent Function (React)

Code


Parent.js

import Child from "./Child";

export default function Parent() {
  function child(name, setName) {
    return (
      <>
        <input
          placeholder="Please Input"
          value={name}
          onChange={(e) => setName(e.target.value)}
        />
      </>
    );
  }

  return (
    <>
      <Child child={child()} />
    </>
  );
}

Child.js

import { useState } from "react";

export default function Child({ child }) {
  const [name, setName] = useState("");
  const [list, setList] = useState([]);

  function onSubmit(e) {
    e.preventDefault();
    if (name === "") {
      console.log("Please enter a name");
    } else {
      setList([...list, name]);
      setName("");
      console.log("Name added to List");
    }
  }

  return (
    <>
      <form onSubmit={onSubmit}>
        {child(name, setName)}
        <button type="submit">Submit</button>
      </form>
    </>
  );
}

Hi, I have been trying to get the data from the child then pass it through parent function.

So, in the child, I want it to pass through the child function so that the Parent can obtain the name, setName, and then send it back to the child so that it can be added to the list. If you have any further questions, please leave them in the comments section below and I will do my best to answer them. Thank you

Codesandbox link: https://codesandbox.io/embed/heuristic-lamarr-u6fzbd?fontsize=14&hidenavigation=1&theme=dark

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Don't invoke the method (child, in your case) while sending it as a prop

import Child from "./Child";

export default function Parent() {
  function child(name, setName) {
    return (
      <>
        <input
          placeholder="Please Input"
          value={name}
          onChange={(e) => setName(e.target.value)}
        />
      </>
    );
  }

  return (
    <>
      <Child child={child} />   {/* pass the child method it like this */}
    </>
  );
}
about 4 years ago · Santiago Gelvez 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!