Holiday Notice: Support will be provided on a limited scale from December 24th, 2024, to January 2nd, 2025. Happy holidays and a wonderful New Year!


Topic: How to hook the size of the component MDBSideNav?

maxchip priority asked 4 days ago


Expected behavior I need to know the size of the component.

Actual behavior I can't get the size of the component (width and height)

Resources (screenshots, code snippets etc.)

enter link description here

import React, { useEffect, useContext, useRef } from 'react';

// MD BootStrap Component
import {
    MDBSideNav,
    MDBSideNavMenu,
    MDBSideNavItem,
    MDBSideNavLink,
    MDBIcon
} from 'mdb-react-ui-kit';

// Data Provider
import { DataContext } from '../data/DataContext';

const ComponentSidebar = () => {

const sidebarRef = useRef(null);

useEffect(() => {
    if (sidebarRef.current) {
        const handleSidebarResize = () => {
            if (sidebarRef.current) {
                const { width, height } = sidebarRef.current.getBoundingClientRect();
                setHeaderSize({ width, height });
            }
        };

        window.addEventListener("resize", handleSidebarResize);
        handleSidebarResize();

        return () => window.removeEventListener("resize", handleSidebarResize);
    }
}, []);

return (
        <>
            {hookSidebarEnable && (
                <MDBSideNav
                    ref={sidebarRef}
                    backdrop={false}
                    absolute
                >
                    <MDBSideNavMenu>
                        <MDBSideNavItem>
                            <MDBSideNavLink>
                                <MDBIcon far icon='smile' className='fa-fw me-3' />
                                Link 1
                            </MDBSideNavLink>
                        </MDBSideNavItem>
                        <MDBSideNavItem>
                            <MDBSideNavLink>
                                <MDBIcon fas icon='grin' className='fa-fw me-3' />
                                Link 2
                            </MDBSideNavLink>
                        </MDBSideNavItem>
                        <MDBSideNavItem>
                            <MDBSideNavLink>
                                <MDBIcon fas icon='grin' className='fa-fw me-3' />
                                Link 3
                            </MDBSideNavLink>
                        </MDBSideNavItem>
                    </MDBSideNavMenu>
                </MDBSideNav>
            )}
        </>
    );
};

export default React.memo(ComponentSidebar);

enter image description here


Mateusz Lazaru staff answered 2 days ago


Sidenav does not support ref forwarding at the moment.

You would need to add some code:.

const navRef = React.useRef<HTMLElement>(null);

useEffect(() => {
  navRef.current = document.querySelector('.sidenav');
  console.log('navRef.current', navRef.current);
}, []);


Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • ForumUser: Priority
  • Premium support: Yes
  • Technology: MDB React
  • MDB Version: MDB5 9.0.0
  • Device: PC
  • Browser: Chrome
  • OS: Windows
  • Provided sample code: No
  • Provided link: Yes