Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

88
Vistas
React rendering a blank page on importing a component that is passed as a prop and giving the error in console

https://codesandbox.io/s/brave-surf-t3todt?file=/src/components/SidebarOption.js:187-194

The app is functioning perfectly up until I don't use the Icon component in the SidebarOption.js. As soon as I put that in there, a blank page is rendered with the console throwing up the following error

expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

SidebarOption.js

import React from "react";
import "../style/sidebarOption.css";

function SidebarOption({ text, Icon }) {
  return (
    <div className="sidebarOption">
      <h2> {text} </h2>
      {/* <Icon/> */}
    </div>
  );
}

export default SidebarOption;

Sidebar.js

import React from 'react'
import '../style/sidebar.css'
import { SidebarOption } from './'
import TwitterIcon from '@mui/icons-material/Twitter';
import HomeIcon from '@mui/icons-material/Home';
import SearchIcon from '@mui/icons-material/Search';
import NotificationsNoneIcon from '@mui/icons-material/NotificationsNone';

function Sidebar() {
  return (
    <div className="sidebar">
        <TwitterIcon />
        <SidebarOption Icon={HomeIcon} text='Home'/>
        <SidebarOption Icon={SearchIcon} text='Explore'/>
        <SidebarOption Icon={NotificationsNoneIcon} text='Notifications'/>
    </div>
  )
}

export default Sidebar;
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I don't know why you're getting that error based on the code you showed here, but in the codesandbox you mentioned, the error is due to you calling the sidebar component twice with no props.

You can fix that error like this by conditional render Icon only if it's passed

function SidebarOption({ text, Icon }) {
  return (
    <div className="sidebarOption">
      <h2> {text} </h2>
      { Icon && <Icon/> }
    </div>
  );
}

export default SidebarOption;
about 4 years ago · Juan Pablo Isaza Denunciar

0

What you are doing is trying to render something that is potentially undefined here: {/* <Icon/> */}. Replace this with {Icon && <Icon />} to check if the prop can be rendered.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda