Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!