Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
How to pass state between functional components in a single file

I have two functional components in a single js file. I want to pass state from one to another

    import React, { useState } from 'react';
    
    export function StepTracker(props) {
      const [steps, setSteps] = useState(0);
    
      function increment() {
        setSteps(prevState => prevState + 1);
      }
    
      return (
        <div>
          Today you've taken {steps} steps!
          <br />
          <button onClick={increment}>
            I took another step
          </button>
        </div>
      );
    }

    export function funcName(props){
return(<div>{state}</div<)
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can do this by

   
   export function StepTracker(props) {
     const [steps, setSteps] = useState(0);
   
     function increment() {
       setSteps(prevState => prevState + 1);
     }
   
     return (
       <div>
         Today you've taken {steps} steps!
         <br />
         <button onClick={increment}>
           I took another step
         </button>
   <FuncName steps={steps}/>
       </div>
     );
   }

   export function FuncName(props){
return(<div>{state}</div<)
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can achieve this by making the other component a child component for the one holding the state.

import React, { useState } from 'react';
import './style.css';

export default function StepTracker() {
  const [steps, setSteps] = useState(0);

  function increment() {
    setSteps((prevState) => prevState + 1);
  }

  return (
    <div>
      <FuncName state={steps} />
      Today you've taken {steps} steps!
      <br />
      <button onClick={increment}>I took another step</button>
    </div>
  );
}

export function FuncName(props) {
  return <div>{props.state}</div>;
}

Another method is to use context. Here's a guide on the official React documentation.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda