<div id="root"></div>
 const useViewport = () => {
  const [width, setWidth] = React.useState(window.innerWidth);

  React.useEffect(() => {
    const handleWindowResize = () => setWidth(window.innerWidth);
    window.addEventListener("resize", handleWindowResize);
    return () => window.removeEventListener("resize", handleWindowResize);
  }, []);

  return {
    width
  };
};

const App = () => {
  const viewPort = useViewport();
  const isMobile = viewPort.width <= 1024;
  if (isMobile) {
    return <h1>Mobile layout</h1>
  }
  return (
    <h1>Desktop layout</h1>
  );
}


ReactDOM.render(<App />,
  document.getElementById("root")
);
View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://unpkg.com/react@17/umd/react.production.min.js
  2. https://unpkg.com/react-dom@17/umd/react-dom.production.min.js