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

122
Views
React collapse pro-sidebar on click

I am a newbie to React. I have a react-pro-sidebar that I want closed/opened on click:

export default function MaterialLayout(props) {
 const { children } = props;
 const classes = useStyle();
 function CollapseSide() {
   document.getElementById('pro1').collapsed=!document.getElementById('pro1').collapsed;
 }
 
 return (
   <ThemeProvider theme={theme}>
     <CssBaseline /> <Header />
     <ProSidebar id='pro1' onClick={CollapseSide} collapsed={false}>
....
....

What am I doing wrong

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

With document.getElementById('pro1').collapsed you're setting the property on the actual rendered DOM Node and not the React Node.

You can instead try the following

export default function MaterialLayout(props) {
 const { children } = props;
 const classes = useStyle();

 const [ sideCollapsed, setSideCollapsed ] = useState(false);
 const toggleSideCollapsed = useCallback(() => {
   console.log('toggle')
   setSideCollapsed(collapsed => !collapsed)
 }, [ setSideCollapsed ]) // could leave this empty
 
 return (
   <ThemeProvider theme={theme}>
     <CssBaseline />
     <Header />
     <ProSidebar onClick={toggleSideCollapsed} collapsed={sideCollapsed}>
....
....

However I'm not sure whether or not ProSidebar takes an onClick property. You can verify that using the console.log statement and checking your console.

For more explanation you can look into the React docs on useState and useCallback or just ask in the comments :)

about 4 years ago · Juan Pablo Isaza Report

0

Could you please try out this code and let me know if it works?

export default function MaterialLayout(props) {
    const { children } = props;
    const classes = useStyle();
    const [toggled, setToggled] = useState(false);
    const [collapsed, setCollapsed] = useState(true);
    const handleToggle = () => {
        setToggled(!toggled);
        setCollapsed(!collapsed);
    }

    return (
        <ThemeProvider theme={theme}>
        <CssBaseline /><Header />
        <ProSidebar collapsed={collapsed} toggled={toggled} onToggle={handleToggle}>
        ...rest of content...
    )
}
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!