Don’t Repeat Yourself
Avoid Hasty Abstractions
all about keeping the balance
const MyWrapperComponent = ({ children, ...otherProps }) => (
<>
{children}
<span>{otherProps?.title}</span>
</>
)
const Layout = ({ children, seoData }) => (
<>
<SEOcomponent data={seoData} />
<HeaderComponent />
<main>
{children}
</main>
<FooterComponent />
</>
)
const Layout = ({ children, seoData, ...otherProps }) => (
<>
<SEOcomponent data={seoData} />
<HeaderComponent {...otherProps.propsForHeader}/>
<main>
{React.cloneElement(props.children, otherProps.moreProps)}
</main>
<FooterComponent {...otherProps.propsForFooter}/>
</>
)
const Comp1 = (layoutProps) => (
<>
<Comp2 {...layoutProps}/>
<Comp3 />
</>
)
const Comp2 = ({ id, foo, bar }) => (
<>
<h2>{id}</h2>
<p className={foo}>{bar}</p>
</>
)
const Layout = ({ children, seoData, ...otherProps }) => (
<DataProvider value={otherProps}>
<SEOcomponent data={seoData} />
<HeaderComponent />
<main>
{children}
</main>
<FooterComponent />
</DataProvider>
)
const Comp1 = (layoutProps) => (
<>
<Comp2 {...layoutProps}/>
<Comp3 />
</>
)
const Comp2 = ({ id, foo, bar }) => (
<>
<h2>{id}</h2>
<p className={foo}>{bar}</p>
</>
)
const Layout = ({ children, seoData, ...otherProps }) => (
<DataProvider value={otherProps}>
<SEOcomponent data={seoData} />
<HeaderComponent />
<main>
{children}
</main>
<FooterComponent />
</DataProvider>
)
const DefaultLayout = ({ children, seoData }) => (
<>
<SEOcomponent data={seoData} />
<HeaderComponent />
<main>
{children}
</main>
<FooterComponent />
</>
)
const DataLayout = (data, otherProps) => (
<DataProvider value={data}>
<DefaultLayout {...otherProps} />
</DataProvider>
)
import { DefaultLayout } from '@layouts/default'
function MyApp({ Component, pageProps }) {
const getLayout =
Component.getLayout ||
(page => <DefaultLayout>{page}</DefaultLayout>)
return getLayout(<Component {...pageProps} />)
}
import { AuthLayout } from '@layouts/auth'
// ...
Page.getLayout = function AthenticatedAuth(page) {
return <AuthLayout>{page}</AuthLayout>
}
export const DefaultLayout = ({ children }) => {
return (
<AuthProvider>
<UserProvider>
<ThemeProvider>
<SpecialProvider>
<JustAnotherProvider>
<VerySpecificProvider>
{children}
</VerySpecificProvider>
</JustAnotherProvider>
</SpecialProvider>
</ThemeProvider>
</UserProvider>
</AuthProvider>
)
}
import { atom, useAtom } from 'jotai'
export const authAtom = atom('value')
import {
AuthAtom,
UserAtom,
ThemeAtom,
SpecialAtom,
JustAnotherAtom,
VerySpecificAtom
} from '@atoms'
export const DEFAULT_VALUES = [
[AuthAtom, 'value1'],
[UserAtom, 'value2'],
[ThemeAtom, 'value3'],
[SpecialAtom, 'value4'],
[JustAnotherAtom, 'value5'],
[VerySpecificAtom, 'value6']
]
import { Provider } from 'jotai'
import { DEFAULT_VALUES } from '@utils/states'
export const DefaultLayout = ({ children }) => {
return (
<Provider initialValues={DEFAULT_VALUES}>
{children}
</Provider>
)
}
runs on every route, only on the server
creates any route
creates an RESTful endpoint
initializes every page
import type { NextApiRequest, NextApiResponse } from 'next'
export default function handler (req: NextApiRequest, res: NextApiResponse) {
res.status(200).json({ name: 'John Doe' })
}
export default function handler (req, res) {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader(
'Cache-Control',
`private, s-maxage=${DAY_IN_SECONDS}, must-revalidate`
)
res.status(200).json({ name: 'John Doe' })
}
https://yourdomain.com/api/endpoint
https://yourdomain.com/api/endpoint-1
https://yourdomain.com/api/endpoint-X
https://yourdomain.com/api/endpoint-1
https://yourdomain.com/api/endpoint-X
https://yourdomain.com/api/
OAuth is a standard apps use to provide apps with “secure delegated access”.
OAuth authorizes devices, APIs, servers, and applications with access tokens rather than credentials
each request carries a JWT, allowing the user to access routes, services, and/or resources
client sends JWT in the Authorization header using the Bearer schema
Authorization: Bearer <token>