简体   繁体   English

如何修复错误:对象作为 React 子对象无效。 如果您打算渲染一组孩子,请改用数组

[英]How to fix error: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead

I'm trying to set the a state with an array of objects but I'm getting the above error and I do not know how to resolve it.我正在尝试使用一组对象设置 a 状态,但出现上述错误,我不知道如何解决它。

My code:我的代码:

const [itemsInCart, setItemsInCart] = useState([]);

  useEffect(() => {
    const fetchData = async () => {
      await fetch("https://fakestoreapi.com/products?limit=16")
        .then((res) => res.json())
        .then((data) => {
          const initialItemCount = data.map((item) => {
            return {
              name: item.title,
              qty: 0,
            };
          });
          setItemsInCart(initialItemCount);
        });
    };

    fetchData();
  }, []);

I tried console.log(Array.isArray(initialItemCount)) to check if it is an array and it returned true.我尝试console.log(Array.isArray(initialItemCount))来检查它是否是一个数组并且它返回了 true。 So why is it saying to use an array?那么为什么说使用数组呢? Thank you谢谢

You can use a breakpoint to see the final value of itemsInCart , to check if this array contains the list of React child component.您可以使用断点查看itemsInCart的最终值,以检查此数组是否包含 React 子组件的列表。

From your code, I guess the array only contains the object of {name and qty}, which can not be rendered directly.从您的代码中,我猜该数组仅包含 {name and qty} 的对象,无法直接呈现。

You may try parsing these objects to a component to render UI.您可以尝试将这些对象解析为组件以呈现 UI。

Hope this help.希望这有帮助。

暂无
暂无

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

相关问题 对象作为 React 子级无效。 如果您打算渲染一组子项,请改用数组 - 错误 Solidity - React - Objects are not valid as a React child. If you meant to render a collection of children, use an array instead - Error Solidity - React “对象作为 React 子对象是无效的。 如果您打算渲染一组子项,请改用数组。” 错误 - “Objects are not valid as a React child. If you meant to render a collection of children, use an array instead.” error 错误:对象作为 React 子项无效。 如果您打算渲染一组孩子,请改用数组 - Error: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead ReactJS 错误:对象作为 React 子项无效。 如果您打算渲染一组孩子,请改用数组 - ReactJS Error: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead 对象作为 React 子项无效。 如果您打算呈现子集合,请改用数组 - Objects are not valid as a React child. If you meant to render a collection of children, use an array instead 未捕获的不变违规:对象无效作为React子代。 如果要渲染子级集合,请改用数组 - Uncaught Invariant Violation: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead 对象作为 React 子对象无效。 如果您打算渲染一组子项,请改用数组 - FlatList - Objects are not valid as a React child. If you meant to render a collection of children, use an array instead - FlatList 错误:对象作为 React 子对象无效。 如果您打算渲染一组子项,请改用数组。 从 JSON 获取数据 - Error: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead. Getting data from JSON 无法显示 ReactJS 中的数据。错误:对象作为 React 子项无效。 如果您打算呈现子集合,请改用数组 - Cannot display data in ReactJS. Error: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead React错误:对象作为React子对象无效,如果要渲染子代集合,请改用数组 - React Error: Objects are not valid as a React child,If you meant to render a collection of children, use an array instead
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM