import React, { useEffect, useState } from 'react';
import ReactDOM from 'react-dom';
import { Switch, Route, BrowserRouter, Link } from 'react-router-dom';
import { BuilderComponent, builder, useIsPreviewing } from '@builder.io/react';
import './index.css';
// Put your API key here
builder.init('YOUR_API_KEY');
function App() {
const [allPages, setAllPages] = useState([]);
useEffect(() => {
async function getStaticProps() {
const pages = await builder.getAll('page', {
fields: 'data.url,name',
options: { noTargeting: true },
});
setAllPages(pages);
}
getStaticProps();
}, []);
return (
} />
);
}
export default function CatchAllRoute() {
const isPreviewingInBuilder = useIsPreviewing();
const [notFound, setNotFound] = useState(false);
const [content, setContent] = useState(null);
// get the page content from Builder
useEffect(() => {
async function fetchContent() {
const content = await builder
.get('page', {
url: window.location.pathname,
})
.promise();
setContent(content);
setNotFound(!content);
}
fetchContent();
}, []);
// if no page is found, return a 404 page
if (notFound && !isPreviewingInBuilder) {
return ; // Your 404 content
}
// return the page when found
return (
<>
{/* Render the Builder page */}
>
);
}
const Home = () =>
I am the homepage!
;
const About = () => I am the about page!
;
const NotFound = () => No page found for this URL, did you publish it?
;
const rootElement = document.getElementById('root');
ReactDOM.render(, rootElement);