简体   繁体   English

我收到一个错误:React.Children.only 预计会在我的 index.js 页面中收到一个 React 元素子项。 是否有解决方法

[英]I am getting an Error: React.Children.only expected to receive a single React element child in my index.js page. Is there a workaround

Index.js code here Index.js 代码在这里


    ReactDOM.render(
      <React.StrictMode>
        <App />
      </React.StrictMode>,
    document.getElementById('root')
    );

I am guessing the error is not comming from the code above and neither is it comming the App.js code.我猜错误不是来自上面的代码,也不是来自 App.js 代码。 App.js code here App.js 代码在这里

        function App() {
          return (
            <div className="App">
              <header className="App-header">
                <AppNavbar/>
                <ShoppingList/>
              </header>
            </div>
          );
       }

Here's an excerpt from ShoppingList.js.这是 ShoppingList.js 的摘录。 Hope this is of help: I think this is where the problem originates because I changed items.map this.state.items.map and the error was, items.map is not defined . Hope this is of help: I think this is where the problem originates because I changed items.map this.state.items.map and the error was, items.map is not defined .

    class ShoppingList extends Component {
        state = {
            items: [
                { id: uuid(), name: 'Eggs'},
                { id: uuid(), name: 'Milk'},
                { id: uuid(), name: 'Apples'},
                { id: uuid(), name: 'Water'},
            ]
        }

    render() {
        const items= this.state;
        return(
            <container>
                <Button
                color="dark"
                style={{marginBottom : '2rem'}}
                onClick={() => {
                    const name = prompt("Enter item");
                    if (name) {
                        this.setState( state => ({
                            items: [...state.items, {id: uuid(), name}]
                        }));

                    }
                }}

                >Add Item</Button>

                <ListGroup>
                    <TransitionGroup className="shopping-list">
                    {this.state.items.map(({ id, name}) => (
                        <CSSTransition key="{id}" timeout="500" classNames="fade">

                        </CSSTransition>
                    ))}
                    </TransitionGroup>
                </ListGroup>

               </container>
        )
      }

    }

I have checked many other errors similar to this but without any success.我检查了许多与此类似的其他错误,但没有任何成功。 Does anyone have a solution for this?有人对此有解决方案吗?

  • Firstly, as per react-transition-group documentation , you will need to put the stuff you need transition inside <CSSTransition> .首先,根据react-transition-group 文档,您需要将需要转换的内容放入<CSSTransition>中。 So in your case, put your ListGroup.Item and Button inside CSSTransition .因此,在您的情况下,将您的ListGroup.ItemButton放在CSSTransition中。
  • Also, you have used wrong key (string) in <CSSTransition key="{id}" .此外,您在<CSSTransition key="{id}"中使用了错误的key (字符串)。 So use provide unique key - like this - <CSSTransition key={id}所以使用提供唯一键 - 像这样 - <CSSTransition key={id}

Working copy of your code is here:您的代码的工作副本在这里:

https://codesandbox.io/s/wild-snow-gwilb?file=/src/ShoppingList.js https://codesandbox.io/s/wild-snow-gwilb?file=/src/ShoppingList.js

Try putting试着放

<AppNavbar/>
<ShoppingList/>

under <div></div> tag as header might need single element under it.<div></div>标签下作为 header 可能需要在它下面的单个元素。

暂无
暂无

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

相关问题 Next.js:错误:React.Children.only expected to receive a single React element child - Next.js: Error: React.Children.only expected to receive a single React element child 获取此 React.Children.only 预计会收到单个 React 元素子错误以供使用<link>在 Next.js - Getting this React.Children.only expected to receive a single React element child error for using <Link> in Next.js ReactQuill(富文本) - 错误:React.Children.only 期望接收单个 React 元素子元素 - React - ReactQuill (rich text) - Error: React.Children.only expected to receive a single React element child - React React bootstrap Tooltip抛出错误&#39;React.Children.only预计会收到一个React元素子元素。 - React bootstrap Tooltip throws error '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. Formik 错误:React.Children.only 期望接收单个 React 元素子元素 - Error: React.Children.only expected to receive a single React element child 未捕获的错误:React.Children.only 期望接收单个 React 元素子项 - Uncaught Error: React.Children.only expected to receive a single React element child React.children.only期望收到一个react元素子Navigator - React.children.only expected to receive a single react element child Navigator 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM