'React App Not rendering With Layout First Approach

I am trying to make a new react app and render this using my layout first approach and there is nothing on my screen. This is the code I used --->

App.js

import React from "react";
import HomeLayoutHOC from "./HOC/Home.Hoc";
import Temp from "./Component/temp";


function App() {
  return (
    <>
      <HomeLayoutHOC path="/" exact component={Temp}/>
    </>
  );
};

export default App;

And then DefaultHOC

import React from "react";
import { Route } from "react-router-dom";
import HomeLayout from "../layout/home.layout";


const HomeLayoutHOC = ({ component: Component, ...rest }) => {
    return (
        <>
            <Route
                {...rest}
                component={(props) => {
                    <HomeLayout>
                        <Component {...props}/>
                    </HomeLayout>
            }}
        />
        </>
    );
};
export default HomeLayoutHOC;

And the home layout

import React from "react";
const HomeLayout = (props) => {
    return (<>
        home Layout
        {props.children}
    </>);
};

export default HomeLayout;

This is the problem I would like to solve, I know this is a version issue or something so I'll add the package.json inside here -->

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.1",
    "@testing-library/react": "^12.1.2",
    "@testing-library/user-event": "^13.5.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-router-dom": "^5.3.0",
    "react-scripts": "5.0.0",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "autoprefixer": "^10.4.2",
    "postcss": "^8.4.5",
    "tailwindcss": "^3.0.16"
  }
}

Please help me fix this issue Goal -> to render the Temp component . in path ="/" using the default HOC . and inside the default layout . as per the layout first approach ,.



Solution 1:[1]

The main issue I see is that your Route component callback function isn't returning anything to render in the HomeLayoutHOC component.

const HomeLayoutHOC = ({ component: Component, ...rest }) => {
  return (
    <>
      <Route
        {...rest}
        component={(props) => {
          <HomeLayout> // <-- no JSX returned!!
            <Component {...props} />
          </HomeLayout>;
        }}
      />
    </>
  );
};

Adding a return statement here allows your code to run without issue.

const HomeLayoutHOC = ({ component: Component, ...rest }) => {
  return (
    <>
      <Route
        {...rest}
        component={(props) => {
          return (
            <HomeLayout>
              <Component {...props} />
            </HomeLayout>
          );
        }}
      />
    </>
  );
};

Edit react-app-not-rendering-with-layout-first-approach

enter image description here

My comment regarding HomeLayoutHOC not really being a Higher Order Component was about it being a wrapper component. Wrapper components take props and "wrap" and render other components, whereas HOCs are higher order functions that consume a React component and return a new, augmented component. You don't ever render a HOC directly as JSX, you pass it a component and render the returned component.

Example of HOC version of HomeLayoutHOC:

const withHomeLayout = (Component) => (props) => (
  <HomeLayout>
    <Component {...props} />
  </HomeLayout>
);

...

const HomeLayoutRoute = withHomeLayout(Route);

...

<HomeLayoutRoute path="/hoc" component={Temp} />

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Drew Reese