Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

97
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda