Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

147
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda