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

156
Vistas
parent component won't show up if I don't pass all the props to the child

First of all, it's good to mention that I'm a bit new to react world. I am using Mui with react. here is my code:

const Search = (props) => {
    const theme = useTheme();
    const isSmall = useMediaQuery(theme.breakpoints.down("sm"));
    return !isSmall || props.show ? <SearchDiv {...props} /> : null;
};

as you can see it's a simple component that under a circumstance returns another custom component. my problem is that if I Don't add {...props} to the child component(SearchDiv) the whole component Search won't show up. but if I add everything works fine and I can't understand why? do we always need to pass all props to any child component?

I searched StackOverflow and google but I didn't find something similar to my question.

Edit : here is the rest of the code:

const SearchDiv = styled("div")(({ theme }) => ({
    position: "relative",
    borderRadius: theme.shape.borderRadius,
    backgroundColor: alpha(theme.palette.common.white, 0.15),
    "&:hover": {
    backgroundColor: alpha(theme.palette.common.white, 0.25),
    },
    marginLeft: 0,
    width: "50%",

   [theme.breakpoints.up("sm")]: {
   marginLeft: theme.spacing(1),
   },
   [theme.breakpoints.down("sm")]: {
   marginLeft: theme.spacing(1),
   },
  }));

const Navbar = () => {
  const [showSearch, setShowSearch] = useState(false);

  return (
    <AppBar>
     <Toolbar sx={{ display: "flex", justifyContent: "space-between"     }}>
        <Search show={showSearch}>

        <StyledInputBase
        placeholder="Search…"
        inputProps={{ "aria-label": "search" }}
        />
        </Search>
        <SearchIcon onClick={() => setShowSearch(true)} />

      </Toolbar>
    </AppBar>
  );
};

export default Navbar;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

For reference, see Composition vs Inheritance.

Without passing props, ie

<SearchDiv />

your styled <div> has no children to render. When you pass all the parent props via {...props}, that includes children, the equivalent of...

<SearchDiv children={props.children} show={props.show} />

I wouldn't recommend this though. An alternative would be

<SearchDiv>
  { props.children }
</SearchDiv>

as per the containment section of the guide linked above.

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