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

194
Visualizações
Date object passed as prop becomes [object Object] instead of [object Date]

A component in my project is calling a function from another file and passing it an object with three properties: stringProp, numProp, and dateProp (which is a Date object the current component receives as a prop from an input component)

const result = MyAPI.calc({stringProp: {stringProp}, numProp: {numProp}, dateProp: {dateProp}})

console.log(`{stringProp: ${stringProp}, numProp: ${numProp}, dateProp: ${dateProp} ${Object.prototype.toString.call(dateProp)}}`)
// >>> {stringProp: something, numProp: 123, dateProp: Fri Jan 19 2024 [object Date]}

When the calc function is called:

export const calc = (props) =>
{
    const stringProp = props.stringProp || ''
    const numProp = props.numProp || 0

    console.log(`props.dateProp: ${props.dateProp} ${Object.prototype.toString.call(dateProp)}`)
    // >>> props.dateProp: [object Object] [object Object]
    ...
}

numProp and stringProp are coming through as expected, but dateProp is not. These three props were already passed as parameters from one component to another, and came through as expected.

In the original input component, dateProp is saved to state with the useState hook.

I've seen a few similar questions, but not many have been answered, or the solution was to fix a typo which I don't think is the case now.

Update: per @KyleRifqi's answer below, calling JSON.stringify(props.dateProp, null, '\t') will return a value like "2024-01-19T00:00:00.000Z" which shows typeof string. How would I convert this into a date? new Date() does not work and returns Invalid Date. There are a lot of older answers using match and/or a reviver function to convert it, but is there a more modern robust approach?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When an object is converted to a string, it is converted as "[object Object]". If you want to console log an object use the JSON.stringify() function.

Example:

console.log(`props.dateProp: ${JSON.stringify(props.dateProp, null, '\t')}`)
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