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

474
Visualizações
Why does e.target return undefined when trying to change an inputs value?

I know this question has been asked before but the solutions posted there didn't correct my issue.

So I'm working with React and have a component. Everything else works fine, but now I'm trying to use a few useStates to get dynamic information as I intend to connect to an API/server further down the line.

I'm also using styled-components, which is why you'll see odd tag names. These work fine because I worked on them first and have them visible when I run npm start

The issue is that when I try to type something other than 0 in to this input, it refuses to update and gives me e.target is undefined

Here's where my error is occuring (on the e.target.value):

<InnerWrap flexColumn>
    <AttributeFrame>

        <EngravingInput 
            type= "text" 
            placeholder="20" 
            inputWidth="50px" 
            name="charSTR" 
            value={charAttributes.charSTR} 
            onChange={(e) => handle_attr_Change(e.target.value)}/>

        <Spacer />
        <>{((charAttributes.charSTR)-10)/2}</>
        <>STR</>
    </AttributeFrame>
</InnerWrap>

Here's the useState (Recently learned I could put multiple fields in a single useState):

    const [charAttributes, set_CharAttributes] = useState(
        {
          charSTR: 0,
          charSTRmod: 0  
        });
    

Then, here's the function that's supposed to fire on the onChange part of my element input:

//This is in my component definition, before the return block
    const handle_attr_Change = (e) => {
        const value = e.target.value;
        set_CharAttributes(
            {
            ...charAttributes,
            [e.target.name]: value
            }
        );

The whole idea is that the charSTRmod value is supposed to update whenever the user types in a number. It's meant to calculate the attribute bonus for a strength attribute from DnD 5e.

Screengrab of the part:

Screenshot showing the elements rendered

Please could someone tell me what I did wrong?

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

0

You can pass the event itself e into handle_attr_Change instead of unpacking it:

<EngravingInput 
  ...
  onChange={handle_attr_Change}
/>
...
const handle_attr_Change = (e) => {
  set_CharAttributes(
    {
       ...charAttributes,
       [e.target.name]: e.target.value
    }
  );
...
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