Next.js Discord

Discord Forum

Error: React.Children.only expected to receive a single React element child.

Unanswered
Asian black bear posted this in #help-forum
Open in Discord
Asian black bearOP
I ma having an issue in the dashboard page:

"use client"

import AQIGauge from "@/components/AQIGauge";
import { AQIData } from "@/types/types";
import React from 'react';
import Sidebar from '@/components/Sidebar';
import NavItem from '@/components/NavItem';
import { FaHome, FaUserAlt, FaCog } from 'react-icons/fa';
import {Box} from "@chakra-ui/react";

const Dashboard: React.FC = () => {
    const aqiData: AQIData = {
        aqi: 75,
        category: "Moderate",
        mainPollutant: "Ozone",
        timestamp: "2024-11-14T10:00:00",
        forecast: {
            today: "Good",
            tomorrow: "Moderate",
        },
    };
    const status = "Moderate";

    return (
        <Box className="flex">
            <Sidebar>
                    <NavItem icon={FaHome} label="Home" key="home" />
                    <NavItem icon={FaUserAlt} label="Profile" key="profile" />
                    <NavItem icon={FaCog} label="Settings" key="settings" />
            </Sidebar>
            <div className="flex-1 p-4">
                <AQIGauge data={aqiData} status={status} />
            </div>
        </Box>
    );
};

export default Dashboard;


when removing the sidebar component.. it is working normally

13 Replies

<Sidebar>
  <>
    <NavItem icon={FaHome} label="Home" key="home" />
    <NavItem icon={FaUserAlt} label="Profile" key="profile" />
    <NavItem icon={FaCog} label="Settings" key="settings" />
  </>
</Sidebar>
try this out @Asian black bear
I am more concerned why is sidebar inside a box, which is imported from chakra
aha It must be. I thought that error comes from his own Sidebar component as I am not familiar with Chakra
<Box className="flex">
  <>
            <Sidebar>
                    <NavItem icon={FaHome} label="Home" key="home" />
                    <NavItem icon={FaUserAlt} label="Profile" key="profile" />
                    <NavItem icon={FaCog} label="Settings" key="settings" />
            </Sidebar>
            <div className="flex-1 p-4">
                <AQIGauge data={aqiData} status={status} />
            </div>
  </>
</Box>
won't this work then? 😓
no, box supports multiple children
then where is the error coming from?
right @Asian black bear check your sidebar component and change the interface
React.Children not React.Children.only
he prolly using something inside the sidebar where he is having multiple children
@James4u then where is the error coming from?
99% due to this
<SomeRadixUIComponent asChild>
  <Something />
  <SomethingElse />
</SomeRadixUIComponent>

which can not be fixed by simply wrapping it inside <>. wrapping components inside <> does nothing.

the problem is likely not in the code OP provided.