Search code examples
reactjscomponentsrendering

React Component Not Showing up as expected


I have a layout component that I am using for a dashboard. After successful login base on the role stored in the jwt the user will be routed to a specific dashboard. The problem I am having is the Dashboard component is not showing on screen. I know the roles are being passed to the dashboard layout. Not sure if its passing down to the dashboard itself ,like expected.

DashboardLayout



import { useEffect,useState } from 'react';
// import { getUserRoles, retrieveToken } from "../Utils/AuthUtils";
import { useNavigate } from 'react-router-dom';
import Sidebarr from '../Components/Sidebarr';
import Navbar from '../Components/Navbar';
import classes from "../Layouts/DashboardLayout.module.css";
import ErrorBoundary from '../ErrorBoundary';
import { useAuth } from '../Utils/AuthProvider';

const DashboardLayout = ({ children}) => {
  // State to management
  const { token, userRoles } = useAuth(); 
  const navigate = useNavigate();
  const [isSidebar, setIsSidebar] = useState(true);

  


  useEffect(() => {
    if (!token && window.location.pathname !== '/api/account/login') {
      navigate('/api/account/login');
    }
  }, [token, navigate]);


  return (
    <>
  
      <ErrorBoundary fallback={<p>Something went wrong</p>}>


      <div className={classes.layout}>
        <main className={classes.content}>
          <Sidebarr isSidebar={isSidebar} />
        <div>
          <Navbar setIsSidebar={setIsSidebar} />
          {children}
          {/* {typeof children === 'function' && children({ userRoles })} */}
        </div>
        </main>
      </div>
      
      </ErrorBoundary>
    </>
      
    
  );
};

export default DashboardLayout;

Dashboard




import { Box } from '@mui/material';
import DashboardLayout from '../Layouts/DashboardLayout';
import Header from '../Components/Header';



const Dashboard = () => {

  return (
    <DashboardLayout>
      {({ userRoles }) => (
        <>
        <Box>
          <Header
            title={userRoles.includes('Overseer') ? 'Admin Dashboard' : 'Group Leader Dashboard'}
            subtitle="Welcome to your dashboard"
            />

        </Box>
          {/* Additional content based on user roles can be added here */}
        

        </>
      )}
    </DashboardLayout>
  );
};

export default Dashboard;

Solution

  • In Dashboard, the children in DashboardLayout is defined as a function. In DashboardLayout, props.children needs to be invoked as a function

    return (
      <ErrorBoundary fallback={<p>Something went wrong</p>}>
        <div className={classes.layout}>
          <main className={classes.content}>
            <Sidebarr isSidebar={isSidebar} />
            <div>
              <Navbar setIsSidebar={setIsSidebar} />
              {children({ userRoles })}
              {/* {typeof children === 'function' && children({ userRoles })} */}
            </div>
          </main>
        </div>
      </ErrorBoundary>
    );