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

184
Visualizações
Cannot read properties of undefined (reading 'target)

first post here. I tried searching around the forum but none of the answers seems to work in my case. I'm pretty new to React, and have an issue with my onChange handler.

In my parent component I have the following:

    // User registration - Onchange handler

    const handleChange = (value, event) => {
      // console.log(event.target.value);
      setFormValue({
        ...formValue,
        [event.target.name]: value,
      });
    };

And basically the parent component renders a multi step registration form, which renders the different components based on a state, here is the conditional for the username:

 {step === 1 ? <Username handleChange={handleChange} formValue={formValue} /> : null}

In the child I have the following input component:

export const Username = ({formValue, handleChange}) => {
  const username = formValue; 
  
    return (
      <View>
      <Input 
      placeholder="Username..."
      onChangeText={handleChange}
      value={formValue.username}
      name={username} />
      </View>
    )
  }

And the input component:

const Input = (props, name) => {

  return (
    <Animated.View style={[styles.inputSection, inputAnimated]}>
        <TextInput style={styles.input} placeholder={props.placeholder} placeholderTextColor={theme.colors.lightP} fontSize={16} {...props}/>
    </Animated.View>
  )
}

I'm not sure why I get the error:

Cannot read properties of undefined (reading 'target)

Would appreciate any help. Console.log of event.target returns undefined.

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

0

This happens because onChangeText passes the input's value automatically to the function that's assigned to it, not passing the event itself

If you want to use the event => use onChange instead of onChangeText

about 4 years ago · Juan Pablo Isaza Relatório

0

Here in the documentation you can read for the onChange event

You are using onChangeText

So onChangeText has just text value, but if you want to have the native event you have to use onChange.

Then access it in you method

    const handleChange = ({ nativeEvent: { eventCount, target, text} }) => {
      // console.log(event.target.value);
      setFormValue({
        ...formValue,
        [target]: text,
      });
    };

EDITED

Since asked again how to access the name of text input. Here this link shows how to handle text input


Also you can do it like this: For example in your method you are giving a name from parameters, you can pass that name to the handler parameter :
export const Username = ({formValue, handleChange}) => {
  const username = formValue; 
  
    return (
      <View>
      <Input 
      placeholder="Username..."
      onChangeText={(value)=>handleChange(value,username)} {/* here username is the name you are passing to the value */}
      value={formValue.username}
      name={username} />
      </View>
    )
  }

// then in your handle method
   const handleChange = (value,name) => {
      setFormValue({
        ...formValue,
        name: 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