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

195
Visualizações
When I output the JavaScript map function to JSX, the value comes out strangely

This is the json array I use.

console.log(materialIngredientDetail)
[
    {
      content: "123",
      functionalList: [
        {
          content: "hi",
          valueUnit: "42",
        },
      ],
    },
    {
        content: "35",
        functionalList: [
          {
            content: "ss",
            valueUnit: "88",
          },
        ],
      },
    {
        content: "456",
        functionalList: [
          {
            content: "minsu",
            valueUnit: "12",
          },
        ],
    },
];

Dynamically generated {Value.value Unit} First button text 42, second button text 88, third button text 12. When these buttons are pressed, the content value is also output.

And I also executed the onClickModifyContent function using Content.current.click() useRef.
The problem is that when a button with a valueUnit value of 42 is clicked, the content is 123, but the output is 456.


  const Content = useRef(null);
  const valueUnit = useRef(null);


  const onClickModifyContent = (content) => {
    console.log(content);
  };

  const onClickModifiyValueUnit = (valueUnit) => {
    Content.current.click();
    console.log(valueUnit);
  };

return (
    <>

    materialIngredientDetail.map((Fields, index) => (
    <>
    <p>{Fields.content}</p>
    <button style={{"display": "none"}} ref={Content} onClick={() => onClickModifyContent(Fields.content)}></button>
    </>

    {Fields.functionalList.map((Value, key) => {
        return (
            <>
            <p>{Value.valueUnit}</p>
            <button onClick={() => onClickModifiyValueUnit(Value.valueUnit)}>{Value.valueUnit}</button>
            </>
        )
    })}
    ))
    </>
)

I'm not sure why the content is 456 when I click the button with valueUnit 42 value.
Similarly, when a button with a valueUnit value of 88 is clicked, 35 Content should be output, but a value of 456 is output. Why the hell are you doing this?

And strangely, the valueUnit value is normally output to console.log.

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

0

That's because you're using single ref for all of your elements. That mean's during the iteration, each element replaces the previous element as the value of Content.current. That's why, no matter what element you click, you only get the last one.

You can use something like this instead:

ref={(el)=>el && Content.current.push(el)}

And on the click function you can pass the index as well and do this:

onClick={()=>modifyValueUnit(Value.valueUnit,index)}

const modifyValueUnit = (unit,index) => {
  Content.current[index].click();
}
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