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

301
Visualizações
How to return true or false individually in code using .map()?

I get the data in the form of an array, and I use the .map() method to manage one state. I want to create an array that changes values ​​individually, but it keeps moving as one. Help me.


    function Follow() {
  const [isFollow, setIsFollow] = useState([]);

  const handleChangeButton = idx => {
    const newIsFollow = [...isFollow];
    newIsFollow[idx] = !newIsFollow[idx];
    setIsFollow(newIsFollow);
  };

  return (
    <FollowWrapper>
      <Title>
        <h5>follw</h5>
      </Title>
      <ul ref={ref}>
        {users.map((info, idx) => {
          return (
            <li key={idx}>
              <UserContainer>
                <UserInfo onClick={() => moveDetailPage(info.product_seq)}>
                  <UserImage
                  />

                  <TextWrapper>
                    <p>
                      <strong>{info.homepage_name}</strong>
                      <br />
                      {info.wholesale_client_id}
                    </p>
                  </TextWrapper>
                </UserInfo>
                <ButtonWrapper>
                  <FollowButton
                    isFollow={isFollow[users.length - 1]}
                    value={users.length - 1}
                    onClick={() => {
                      handleChangeButton(users.length - 1);
                    }}
                  >
                    follw
                  </FollowButton>
                </ButtonWrapper>
              </UserContainer>
            </li>
          );
        })}
      </ul>
      <NavHeightBox />
    </FollowWrapper>
  );
}

How can I get the data and manage the state values ​​individually? I tried to designate the state value individually, but it doesn't seem like a good way because I don't know how many data will come in.

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

0

You don't appear to be passing the correct "index" value to your FollowButton props or the handleChangeButton. You are effectively passing the last index (users.length - 1) to all of them. Use the mapped idx value.

const handleChangeButton = idx => {
  const newIsFollow = [...isFollow];
  newIsFollow[idx] = !newIsFollow[idx]; // <-- passed index
  setIsFollow(newIsFollow);
};

...

{users.map((info, idx) => {            // <-- current index
  return (
    <li key={idx}>
      <UserContainer>
        ...
        <ButtonWrapper>
          <FollowButton
            isFollow={isFollow[idx]}   // <-- current index
            value={idx}                // <-- current index
            onClick={() => {
              handleChangeButton(idx); // <-- current index
            }}
          >
            follow
          </FollowButton>
        </ButtonWrapper>
      </UserContainer>
    </li>
  );
})}

Update

Since the isFollow array is initially empty, you can update any index you please and it will lead to undefined "holes" in the array until you "fill them in" by toggling the value there. undefined is a falsey value, so the negation is a truthy value. If you desire you can initialize the isFollow state to match the users array.

const [isFollow, setIsFollow] = useState(Array(users.length).fill('N'));

...

const handleChangeButton = idx => {
  const newIsFollow = [...isFollow];
  newIsFollow[idx] = newIsFollow[idx] === "Y' ? 'N' : 'Y';
  setIsFollow(newIsFollow);
};

IMO it would be simpler and more trivial to just keep isFollow a boolean type for easy toggling and derive in the UI what the "Y"/"N" value should be from the boolean state values.

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