简体   繁体   English

setState 在本机反应中不是 function

[英]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.我正在学习 React Native,在 React Native 中遇到此错误setState is not a function我搜索了很多但没有任何帮助。

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这是错误: TypeError: Test1.setState is not a function

Please help me fix this.请帮我解决这个问题。

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.你需要从 App 调用 Test1 组件,从 Test1 调用 Test 组件,然后你可以将 props 从 Test1 传递给 Test。 By this you don't need to move the state to other component.这样您就不需要将 state 移动到其他组件。 you can not pass any component as props and access state or methods from there.您不能将任何组件作为道具传递并从那里访问 state 或方法。 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;
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;

There are two problems here.这里有两个问题。

  1. Your Test1 component is not being used at all您的 Test1 组件根本没有被使用
  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.如果您想在您的测试组件中管理一些本地 state,它需要存在于该组件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM