简体   繁体   English

React.Children.only只希望收到一个React元素chil

[英]React.Children.only expected to receive a single React element chil

I'm toying around trying to get a cat API to work learning reactjs, this error keeps being thrown - I've searched a while to no prevail. 我在玩弄设法让猫API工作以学习reactjs的方法,这个错误不断抛出-我搜索了一阵子没有成功。 The error keeps pointing to L21 where I do an api request to and process the data to state, I can't see what would cause this to fail as was working previously? 该错误一直指向L21,我在该处执行api请求并处理数据以声明状态,我看不到是什么原因导致此操作无法正常工作?

I've tried changing fragments for divs etc. 我试过更改div等的片段

I've tried moving things around, deconstructing and I am at my ends wit! 我已经尝试过移动事物,进行解构,但我的机智终究到了!

import React, { Component } from 'react'
import Loading from '../Home/Loading'
import { BrowserRouter as Link } from "react-router-dom";

import { Col } from 'react-bootstrap'

const API_BREEDS = 'https://api.thecatapi.com/v1/breeds'

export default class BreedList extends Component {
    constructor() {
        super();
        this.state = {
            breeds: [],
            isLoading: true,
            error: null,
        };
    }
    fetchBreeds() {
        fetch(API_BREEDS)
            .then(response => response.json())
            .then(data =>
                this.setState({
                    breeds: data,
                    isLoading: false,
                }))
            .catch(error => this.setState({ error, isLoading: false }));
    }
    componentDidMount() {
        this.fetchBreeds();
        // console.log(this.state)
    }
    render() {
        const { isLoading, breeds, error } = this.state;
        return (
            <React.Fragment>
                {error ? <p>{error.message}</p> : null}
                {!isLoading ? (
                    breeds.map(feline => {
                        const { id, name } = feline;
                        return (<Col key={id} md={4} className="card p-2" >
                                <Link to={"/api/" + id}>{name}</Link>
                            </Col>
                        );
                    })
                ) : (
                        <Loading />
                    )}
            </React.Fragment>
        )
    }
}

Error: 错误:

Uncaught Error: React.Children.only expected to receive a single React element child.
    at invariant (react.development.js:105)
    at Object.onlyChild [as only] (react.development.js:1287)
    at Router.render (Router.js:118)
    at finishClassComponent (react-dom.development.js:15141)
    at updateClassComponent (react-dom.development.js:15096)
    at beginWork (react-dom.development.js:15980)
    at performUnitOfWork (react-dom.development.js:19102)
    at workLoop (react-dom.development.js:19143)
    at HTMLUnknownElement.callCallback (react-dom.development.js:147)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:196)
    at invokeGuardedCallback (react-dom.development.js:250)
    at replayUnitOfWork (react-dom.development.js:18350)
    at renderRoot (react-dom.development.js:19261)
    at performWorkOnRoot (react-dom.development.js:20165)
    at performWork (react-dom.development.js:20075)
    at performSyncWork (react-dom.development.js:20049)
    at requestWork (react-dom.development.js:19904)
    at scheduleWork (react-dom.development.js:19711)
    at Object.enqueueSetState (react-dom.development.js:12936)
    at BreedList.push../node_modules/react/cjs/react.development.js.Component.setState (react.development.js:356)
    at BreedList.js:21

Welcome to the boards Bread! 欢迎来到板面包! :D :d

Looking at your render code, you could probably reformat it to something like this: 查看您的渲染代码,您可能将其重新格式化为如下所示:

render() {
    const { isLoading, breeds, error } = this.state;
    return (
           {error ? <p>{error.message}</p> : null}
           {!isLoading ? (
                breeds.map(feline => {
                    const { id, name } = feline;
                    return (
                       <React.Fragment key={id}>
                           <Col md={4} className="card p-2" >
                               <Link to={"/api/" + id}>{name}</Link>
                           </Col>
                       </React.Fragment>
                    );
                })
            ) : (
               <Loading />
              )}
    )
}

That way your .map is actually returning a fragment for each item. 这样,您的.map实际上将为每个项目返回一个片段。

暂无
暂无

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

相关问题 我收到“错误:React.Children.only expected to receive a single React element child.” 与 TouchableWithoutFeedback - I receive "Error: React.Children.only expected to receive a single React element child." with TouchableWithoutFeedback React bootstrap Tooltip抛出错误&#39;React.Children.only预计会收到一个React元素子元素。 - React bootstrap Tooltip throws error 'React.Children.only expected to receive a single React element child.' ReactQuill(富文本) - 错误:React.Children.only 期望接收单个 React 元素子元素 - React - ReactQuill (rich text) - Error: React.Children.only expected to receive a single React element child - React React Router v4 - 错误:React.Children.only预计会收到一个React元素子元素 - React Router v4 - Error: React.Children.only expected to receive a single React element child React.Children.only 期望接收单个 React 元素孩子。? - React.Children.only expected to receive a single React element child.? WordPress的GutenBurg块-React.Children。仅预期接收一个React子元素 - Wordpress GutenBurg Block - React.Children.only expected to receive a single React element child Nextjs - Reactjs - 不变违规:React.Children.only 预期接收单个 React 元素子元素 - Nextjs - Reactjs - Invariant Violation: React.Children.only expected to receive a single React element child 错误 React.Children.only 期望接收单个 React 元素子元素 - Error React.Children.only expected to receive a single React element child 不变违规:React.Children.only预计会收到一个React元素子 - Invariant Violation: React.Children.only expected to receive a single React element child React.Children.only 预计会收到单个 React 元素子错误 - React.Children.only expected to receive a single React element child error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM