Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!