-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathcustom-child.tsx
57 lines (51 loc) · 1.27 KB
/
custom-child.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Quickstart snippet
* snippets/react/routes/custom-child.tsx
*/
import {
Content,
fetchOneEntry,
isPreviewing,
type BuilderContent,
} from '@builder.io/sdk-react';
import { useEffect, useState } from 'react';
import { customHeroInfo } from '../../components/CustomHero';
const BUILDER_API_KEY = 'ee9f13b4981e489a9a1209887695ef2b';
const MODEL_NAME = 'page';
export default function CustomChildRoute() {
const [notFound, setNotFound] = useState(false);
const [content, setContent] = useState<BuilderContent | null>(null);
// get the page content from Builder
useEffect(() => {
fetchOneEntry({
model: MODEL_NAME,
apiKey: BUILDER_API_KEY,
userAttributes: {
urlPath: window.location.pathname,
},
})
.then((content) => {
if (content) {
setContent(content);
}
setNotFound(!content);
})
.catch((err) => {
console.log('Oops: ', err);
});
}, []);
// If no page is found, return
// a 404 page from your code.
if (notFound && !isPreviewing()) {
return <div>404</div>;
}
// return the page when found
return (
<Content
content={content}
model={MODEL_NAME}
apiKey={BUILDER_API_KEY}
customComponents={[customHeroInfo]}
/>
);
}