简体   繁体   English

尝试响应 MAP 数据时出现错误。 对象作为 React 子对象无效(找到:object 和键 {children}),我该如何解决?

[英]I AM HAVING ERROR WHEN TRYING TO MAP A DATA IN REACT . Objects are not valid as a React child (found: object with keys {children}), How do i solve it?

I am trying to map through an array of JSON data and I keep getting this error in the title of this question.我正在尝试通过 JSON 数据数组来 map 并且我在这个问题的标题中不断收到这个错误。 The Accordion component is what makes up the faqs.js, i am using styled-components. Accordion 组件构成了 faqs.js,我使用的是 styled-components。 The faq.json contains the data I want to map in the faq.js file Below is my code faq.json 在 faq.js 文件中包含我想要的数据 map 下面是我的代码

JSON DATA JSON 数据

[
  {
    "id": 1,
    "header": "What is Netflix?",
    "body": "Netflix is a streaming service that offers a wide variety of award-winning TV programmes, films, anime, documentaries and more – on thousands of internet-connected devices.\n\nYou can watch as much as you want, whenever you want, without a single advert – all for one low monthly price. There's always something new to discover, and new TV programmes and films are added every week!"
  },
  {
    "id": 2,
    "header": "How much does Netflix cost?",
    "body": "Watch Netflix on your smartphone, tablet, smart TV, laptop or streaming device, all for one low fixed monthly fee. Plans start from £5.99 a month. No extra costs or contracts."
  },
  {
    "id": 3,
    "header": "Where can I watch?",
    "body": "Watch anywhere, anytime, on an unlimited number of devices. Sign in with your Netflix account to watch instantly on the web at netflix.com from your personal computer or on any internet-connected device that offers the Netflix app, including smart TVs, smartphones, tablets, streaming media players and game consoles.\n\nYou can also download your favourite programmes with the iOS, Android, or Windows 10 app. Use downloads to watch while you're on the go and without an internet connection. Take Netflix with you anywhere."
  },
  {
    "id": 4,
    "header": "How do I cancel?",
    "body": "Netflix is flexible. There are no annoying contracts and no commitments. You can easily cancel your account online in two clicks. There are no cancellation fees – start or stop your account at any time."
  },
  {
    "id": 5,
    "header": "What can I watch on Netflix?",
    "body": "Netflix has an extensive library of feature films, documentaries, TV programmes, anime, award-winning Netflix originals, and more. Watch as much as you want, any time you want."
  }
]

FAQS JS ()WHERE I AM TRYING TO MAP OVER THE JS常见问题解答 JS ()我在哪里尝试通过 JS 使用 MAP

import React from 'react'
import { Accordion } from "../components";
import faqsData from '../fixtures/faq.json';

export default function FaqsContainers() {
    return (

        <Accordion>
            <Accordion.Title>Frequently Asked Questions</Accordion.Title>

            {faqsData.map((item) => (
                <Accordion.Item key={item.id}>
                    <Accordion.Header>{item.header}</Accordion.Header>
                    <Accordion.Body>{item.body}</Accordion.Body>
                </Accordion.Item>
                ))}
            <Accordion.Item />
        </Accordion>


    )
}

ACCORDION COMPONENTS手风琴组件

import React, {useState , useContext , createContext} from 'react';
import { Container , Frame , Title, Item, Inner, Header, Body } from './style/accordion';

const ToggleContext = createContext();

export default function Accordion ({ children , ...restProps}){
    return (
        <Container>
            <Inner {...restProps}>
                {children}
            </Inner>
        </Container>
    );
}

Accordion.Title = function AccordionTitle(children, ...restProps) {
    return <Title {...restProps}>{children}</Title>;
};
Accordion.Frame = function AccordionFrame(children, ...restProps) {
    return <Frame {...restProps}>{children}</Frame>;
};
Accordion.Item = function AccordionItem(children, ...restProps) {
    const [toggleShow, setToggleShow] = useState(false)
    return <Item {...restProps}>{children}</Item>;
};
// setToggle function is used to control the toggling of the accodion  

Accordion.Header = function AccordionHeader(children, ...restProps) {
    const {toggleShow , setToggleShow} = useContext(ToggleContext)
    return <Header onClick={()=> setToggleShow((toggleShow)=> !toggleShow)} 
    {...restProps}>{children}
    </Header>;
};

Accordion.Body = function AccordionBody(children, ...restProps) {
    const {toggleShow} = useContext(ToggleContext)
    return toggleShow ? <Body{...restProps}> {children} </Body> : null ;
};

To fix the error, you will need to change all your Accordion.xxx elements to destructure the props passed to them.要修复错误,您需要更改所有Accordion.xxx元素以解构传递给它们的道具。 So this所以这

Accordion.Title = function AccordionTitle(children, ...restProps) {

needs to become this:需要变成这样:

Accordion.Title = function AccordionTitle({children, ...restProps}) {

and this:和这个:

Accordion.Frame = function AccordionFrame(children, ...restProps) {

should turn into:应该变成:

Accordion.Frame = function AccordionFrame({children, ...restProps}) {

And so on...等等...

暂无
暂无

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

相关问题 React,错误:对象作为 React 子项无效(找到:object 和键 {data}) - React , Error: Objects are not valid as a React child (found: object with keys {data}) React 错误,当我在孩子中使用 Redux 连接时发生,如何绕过?对象作为 React 孩子无效 - React Error, happens when I use Redux connect in a children, how to bypass?Objects are not valid as a React child 即使我没有尝试渲染对象,获取对象也不能作为 React 子错误有效 - Getting a Objects are not valid as a React child error even though I am not trying to render an object 我收到一个错误:对象作为 React 子对象无效(找到:[object Promise]) - I got an Error: Objects are not valid as a React child (found: [object Promise]) 如何解决:对象作为 React 子对象无效(发现:object 带有键 {_U、_V、_W、_X}? - How to solve: Objects are not valid as a React child (found: object with keys {_U, _V, _W, _X}? 对象作为React子对象无效(找到:带有键{…}的对象) - Objects are not valid as a React child (found: object with keys {…}) 对象作为 React 子对象无效(找到:带有键的对象...) - Objects are not valid as a React child (found: object with keys…) 对象无效作为React子对象(找到:带有键的对象) - Objects are not valid as a React child (found: object with keys) 对象作为 React 子项无效(找到:object,键为 {}) - Objects are not valid as a React child (found: object with keys {}) 错误:对象作为React子对象无效(找到:带有键的对象…) - Error: Objects are not valid as a React child (found: object with keys…)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM