Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

222
Vistas
setState is not a function in react native

I am learning react native, have been getting this error setState is not a function in react native I searched a lot but nothing was helpful enough.

I have created this simplified code to show the issue

import React, { useState } from "react";
import { Text, View, Button } from "react-native";

const Test = ({ Test1 }) => {
  return (
    <Button
      onPress={() => {
        Test1.setState(true);
      }}
    />
  );
};

const Test1 = () => {
  const [state, setState] = useState(false);
  if (state) {
    return <Text>Test Working</Text>;
  } else {
    return <Text>Test Not Working</Text>;
  }
};

const App = () => {
  return (
    <View>
      <Test Test1={Test1} />
    </View>
  );
};

export default App;

this is the error: TypeError: Test1.setState is not a function

Please help me fix this.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

States can be transferred to other component only as props. You need to call the Test1 component from the App and the Test component from the Test1, then you can pass the props to the Test from Test1. By this you don't need to move the state to other component. you can not pass any component as props and access state or methods from there. You can try this code:

import React, { useState } from "react";
import { Text, View, Button } from "react-native";

const Test = ({ setState}) => {
  return (
    <Button
      onPress={() => {
        setState(true);
      }}
    />
  );
};

const Test1 = () => {
  const [state, setState] = useState(false);

  if (state) {
    return <Text>Test Working</Text>;
  } else {
    return <Test setState={setState} />;
  }
};

const App = () => {
  return (
    <View>
      <Test1  />
    </View>
  );
};

export default App;
about 4 years ago · Juan Pablo Isaza Denunciar

0

import React, { useState } from "react";
import { Text, View, Button } from "react-native";

const Test = ({ setState }) => {
  return (
    <Button
      onPress={() => {
           setState(true);
      }}
  );
};

const Test1 = ({state}) => {
 
  if (state) {
    return <Text>Test Working</Text>;
  } else {
    return <Text>Test Not Working</Text>;
  }
};

const App = () => {
    
  const [state, setState] = useState(false);
  return (
    <View>
        <Test1 state={state} />
      <Test setState={setState} />
    </View>
  );
};

export default App;
about 4 years ago · Juan Pablo Isaza Denunciar

0

There are two problems here.

  1. Your Test1 component is not being used at all
  2. Hooks, and local functions in general, may not be called outside of the component that they are declared on

If you want to manage some local state in your Test component, it needs to live in that component.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda