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

206
Visualizações
React Native onChange undefined

so I am trying to perform some onChange event, as I learned, but somehow it throws me the error that the onChange function is undefined. The right dependency is installed (react-hook-form).

Here is my code.

Someone a idea, where the issue is coming from?

https://codesandbox.io/embed/jolly-butterfly-diqb1?fontsize=14&hidenavigation=1&theme=dark

import React from "react";
import {
  StyleSheet,
  TextInput,
  Text,
  TouchableOpacity,
  Image,
  ScrollView
} from "react-native";
import { useForm, Controller } from "react-hook-form";

const AddAddress = () => {
  const { control, handleSubmit, errors, reset } = useForm({
    defaultValues: {
      name: "",
      email: ""
    }
  });

  function submit(data) {
    console.log(data);
  }

  return (
    <ScrollView contentContainerStyle={styles.container}>
      <Text style={styles.title}>React Hook Form</Text>
      <Controller
        control={control}
        name="name"
        render={({ onChange, value }) => (
          <TextInput
            placeholder="Name"
            style={styles.input}
            onChangeText={(value) => onChange(value)}
          />
        )}
      />
      <Controller
        control={control}
        name="email"
        render={({ onChange, value }) => (
          <TextInput
            placeholder="Email"
            style={styles.input}
            onChangeText={(value) => onChange(value)}
          />
        )}
      />
      <TouchableOpacity onPress={handleSubmit(submit)}>
        <Text style={styles.button}>Submit</Text>
      </TouchableOpacity>
    </ScrollView>
  );
};

export default AddAddress;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It is because you are passing the onChange function dirctly, but it is a part of the field prop, so you should pass them as a named argument

...Controller
render={({
field: { onChange, onBlur, value, name, ref },
fieldState: { invalid, isTouched, isDirty, error },
}) => ( ...

Kindly have a look at the docs : https://react-hook-form.com/api/usecontroller/controller

Or pass in the value field, and use the field.onChange method.

This is the modified code.

The output on submit will be as follows after providing the inputs
{name: "sample", email: "sample@g,co"}

import React from "react";
import {
  StyleSheet,
  TextInput,
  Text,
  TouchableOpacity,
  Image,
  ScrollView
} from "react-native";
import { useForm, Controller } from "react-hook-form";

const AddAddress = () => {
  const { control, handleSubmit, errors, reset } = useForm({
    defaultValues: {
      name: "",
      email: ""
    }
  });

  function submit(data) {
    console.log(data);
  }

  return (
    <ScrollView contentContainerStyle={styles.container}>
      <Text style={styles.title}>React Hook Form</Text>
      <Controller
        control={control}
        name="name"
        // ------------ modified here
        render={({field}) => (
          <TextInput
            placeholder="Name"
            style={styles.input}
            onChange = {(e)=>field.onChange(e)}
            value = {field.value}
          />
        )}
      />
      <Controller
        control={control}
        name="email"
        // ------------ modified here
        render={({ field }) => (
          <TextInput
            placeholder="Email"
            style={styles.input}
            onChange = {(e)=>field.onChange(e)}
            value = {field.value}
          />
        )}
      />
      <TouchableOpacity onPress={handleSubmit(submit)}>
        <Text style={styles.button}>Submit</Text>
      </TouchableOpacity>
    </ScrollView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#282828",
    alignItems: "center",
    justifyContent: "center"
  },
  title: {
    fontSize: 36,
    marginBottom: 30,
    marginTop: 16,
    color: "white"
  },
  error: {
    fontSize: 16,
    color: "red",
    marginTop: 16,
    marginBottom: 16,
    marginLeft: 36,
    marginRight: 36
  },
  input: {
    fontSize: 18,
    borderWidth: 1,
    padding: 12,
    width: "80%",
    borderRadius: 10,
    backgroundColor: "white",
    marginBottom: 16,
    marginTop: 16
  },
  image: {
    width: 120,
    height: 120,
    borderColor: "orange",
    borderWidth: 2,
    borderRadius: 100
  },
  button: {
    fontSize: 20,
    color: "white",
    width: 120,
    marginTop: 8,
    borderRadius: 10,
    backgroundColor: "#c01c00",
    padding: 8,
    textAlign: "center"
  }
});

export default AddAddress;

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