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

95
Views
How to pass a dynamic value from parent to child in React?

So my App.js file looks like this:

import React from "react";
import { useState } from "react";
import './App.css';
import Header from "./components/pages/Header";
import NAV from "./NAV";
import Trees from "./components/tree";


function App() {
 
    
      const [inputField , setInputField] = useState({
        first_name: 'Phyag_NZFS3770'
    })
   
    const inputsHandler = (e) =>{
        setInputField( {[e.target.name]: e.target.value} )
    }

    const submitButton = () =>{
        alert(inputField.first_name)
        

    }
  


return(
<div className="App">
      <NAV genomename={inputField.first_name}/> //this can't be done as the prop value is undefined
      <Header title="EumicrobeDB"/>
      <h3 style={{ textAlign:"left" }}> Organism List </h3>  
        <div> <Trees /> </div>
          <div>
            <input 
            type="text" 
            name="first_name" 
            onChange={inputsHandler} 
            placeholder="enter the organism id" 
            value={inputField.first_name}/>
            <button onClick={submitButton}>Submit Now</button> 
        </div>
    </div>
    )

    
  } 
export default App;

Can anyone please modify the code to pass {inputField.first_name} as prop value to my function called NAV? I am in an absolute fix..I am a biologist who is new to React; who has to meet a deadline

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

0

This is how you can pass data from parent component to child as a props.

Example

import React from "react";
import { useState } from "react";

const Header = ({ title }) => {
  return <React.Fragment>Hello, {title}</React.Fragment>;
};

function App() {
  const [inputField, setInputField] = useState({
    first_name: "Phyag_NZFS3770"
  });

  const inputsHandler = (e) => {
    console.log([e.target.name] , e.target.value);
    setInputField({ [e.target.name]: e.target.value });
  };

  const submitButton = () => {
    alert(inputField.first_name);
  };

  return (
    <div className="App">
      <Header title={inputField.first_name} />
      <h3 style={{ textAlign: "left" }}> Organism List </h3>
      <div>
        <input
          type="text"
          name="first_name"
          onChange={inputsHandler}
          placeholder="enter the organism id"
          value={inputField.first_name}
        />
        <button onClick={submitButton}>Submit Now</button>
      </div>
    </div>
  );
}
export default App;
about 4 years ago · Juan Pablo Isaza Report

0

Your code in this component looks fine as-is. Make sure you're accessing props.genomename and not props.first_name in the NAV component.

If you're planning to add more input fields, I'd also suggest you change the inputHandler function to merge with the previous state, rather than replacing it entirely:

const inputsHandler = (e) => {
    setInputField({ ...inputField, [e.target.name]: 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!