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

189
Vistas
React: Pass result of function (return) as props. But Child component receives as undefined?

I have the following setup where I have a function tomorrowDate() that returns a date. I want to pass this as a prop. But on the Child component this is undefined. I assume that there is something going wrong with passing the returned value.

How should I do that?

const Parent = () => {
    const tomorrowDate = () => {
        return new Date()
    }


    return (

        <Child
        minDate={tomorrowDate()}
        test={"test"}
        />
    )
}


class Child extends Component {

    componentWillMount() {
        console.log("props are", this.props)
    }

    render(){
        // DO STH
    }
}

The output is...

props are {test:"test", minDate=undefined}

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I just copy and pasted your code and if you see the result of this snippet, it's working without any problem.

const Parent = () => {
  const tomorrowDate = () => {
    return new Date()
  }

  return (
    <Child 
    minDate = {tomorrowDate()}
    test = {"test"}
    />
  )
}

class Child extends React.Component {
  componentWillMount() {
    console.log("props are", this.props)
  }

  render() {
    return null; // DO STH
  }
}

ReactDOM.render( <Parent / > , document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

I'm not sure what the issue is but I've created a snippet that does what you want.

function Child({ minDate }) {
  return <div>{minDate.toLocaleString()}</div>;
}

function Parent() {
  const tomorrowDate = () => {
    return new Date();
  };

  return (
      <Child minDate={tomorrowDate()} />
  );
}

// Ignore this
ReactDOM.render(
  <Parent />,
  document.getElementById("react")
);
<div id="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

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