首页 > 解决方案 > 对象作为 React 子级无效 - React Hooks

问题描述

我正在尝试从休息端点获取所有项目。但不幸的是,我得到了一个错误。有人可以看看我的代码并告诉我哪里出错了。这是 JSON 数据。我正在使用 React Hooks。并从本地休息端点获取数据以获取项目详细信息。

    const [data, setData] = useState();
    const [isLoading, setIsLoading] = useState(false);
    const [isError, setIsError] = useState(false);


    useEffect(() => {
        const fetchData = async () => {
            setIsError(false);
            setIsLoading(true);

            try {
                const result = await axios('http://192.168.0.176:8080/projectses');

                setData(result.data);
            } catch (error) {
                setIsError(true);
            }

            setIsLoading(false);
        };

        fetchData();
    }, []);

const { colors } = useTheme();
const theme = useTheme();

return (
    <View style={styles.container}>
        
      <Text>{data}</Text>

      </View>
   
);



  "project_ID" : 2,

  "project_NAME" : "Project B",

  "_links" : {

    "self" : {

      "href" : "http://192.168.0.176:8080/projectses/2"

    },

    "projects" : {

      "href" : "http://192.168.0.176:8080/projectses/2"

    }

  }

标签: reactjsreact-nativereact-hooks

解决方案


我认为如果数据是数组,你可以像这样映射:

return (
  <View style={styles.container}>
    {
      data.map((item) => <Text>{item.project_ID}</Text>)
    }
  </View>
)

推荐阅读