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

508
Visualizações
ReactJs: Uncaught TypeError: Cannot read properties of undefined (reading '0')

I am trying to access the key and values in payload in my JSON data, and if the key is present in array, countTargetOptions, I will display it in a component.

But I keep getting a Uncaught TypeError: Cannot read properties of undefined (reading '0') when I try to display the data {payload[0]?.payload?.countTargetOptions[i]} (note: I added the ? after reading React: Uncaught TypeError: Cannot read properties of undefined (reading '0') but it is still not working).

Before I tried to render the components in a loop, there was no issues. See below my code and a photo of the payload data.

const countTargetOptions = [
  "count_targets",
  "count_targets_excluded",
  "count_targets_pending",
  "count_targets_in_progress",
  "count_targets_completed",
  "count_targets_failed",
];

const CustomTooltip = ({ payload}: TooltipProps<ValueType, NameType> | any) => {

    if( payload && payload.length){
      var targetsData = [];
      let payloadData: any = payload[0].payload;
      for(let i = 0; i < countTargetOptions.length; i++){
        if(payloadData.hasOwnProperty(countTargetOptions[i])){
          let targetOption: string  = countTargetOptions[i].replaceAll('_', ' ');         
          targetsData.push(
          <Typography key = {targetOption} sx={{ fontSize: 14 }} color={"black"} >
          {targetOption} : {payload[0]?.payload?.countTargetOptions[i]}
        </Typography> )

        }
      }
      return (
        <>
        <Card>
          <CardContent>
            {targetsData}

            //Example of what wored before loop
            {/* <Typography sx={{ fontSize: 14 }} color={"black"} >
              count targets : {payload[0].payload.count_targets}
            </Typography> */}
          </CardContent>
        </Card>
        </>
      );
    }
    return null;
  }

enter image description here

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

0

You probably wanted to do this:

{targetOption} : {payload[0].payload[countTargetOptions[i]]

About ?.

?. is an operator that returns undefined if the left-hand side is undefined. You added it in the wrong places. Doing this:

{targetOption} : {payload[0].payload.countTargetOptions?.[i]}

would be a little bit better, because you wouldn't get the error then, although the value would still be undefined.

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