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

159
Visualizações
Can't pass target.value to state hook return undefined - React.js React

When clicked I want to pass name prop to state hook but it returns undefined

    const [fav, setFav ] = useState([]); 
    
         useEffect(() => {
        dispatch(fetchProfileAction(user));
        dispatch(fetchReposAction(user));
      }, [user, dispatch]);
    
            {reposList?.name !== "Error" &&
          reposList?.map(repo => (
            <>
                <div className="col-6 g-3 ">
                <div className="border rounded-pill px-3 py-2 bg-secondary  ">
                <i 
                class="far fa-star pe-2" role={ 'button'}
                name={repo?.name} role={'button'}
                onClick={e => setFav(...fav, e.target.name)}
                ></i> 
                <a
                    target="_blank"
                    href={repo?.html_url}
                    className="text-light"
                    name={repo?.name} role={'button'}
                    onClick={e => setFav(...fav, e.target.name)}
                  >
                    {repo?.name} 
                  </a>
                  <strong className="text-light ps-2">({repo?.language})</strong>
              </div>
            </div>
                  
                
            </>
          ))}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If you want to append a the name to the existing array of fav, then use functional updates instead:

onClick={e => setFav(fav => [...fav, e.target.name])}

It is recommend that states should be treated as readonly: any modifications to the current state that is based on the previous state should use the functional update pattern. This allows React to batch changes to the state together: see further explanation in this answer.

about 4 years ago · Juan Pablo Isaza Relatório

0

According to https://docs.w3cub.com/dom/element/name name officially only applies to the following elements: <a>, <applet>, <button>, <form>, <frame>, <iframe>, <img>, <input>, <map>, <meta>, <object>, <param>, <select>, and <textarea>.

So, since you have used the name attribute on i tag, you should try to get the name value by getAtrribute, like this:

 <i 
   class="far fa-star pe-2" role={ 'button'}
   name={repo?.name}
   role={'button'}
   onClick={e => setFav( [...fav, e.target.getAttribute('name')] )}
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