Dynamic generated theme from API #1577
Answered
by
lachlanjc
shuyang0129
asked this question in
Q&A
-
I want to change the primary color depend on different clients. This is the development flow:
How should I do that? |
Beta Was this translation helpful? Give feedback.
Answered by
lachlanjc
Mar 18, 2021
Replies: 1 comment 5 replies
-
Your exact implementation will depend a lot on the framework you're using & how you're architecting your app. However, if you wanted to do this in Next.js, here's how I might go about it: // pages/_app.js
import * as React from 'react'
import { useRouter } from 'next/router'
import defaultTheme from '../lib/DEFAULT-THEME'
import { ThemeProvider } from 'theme-ui'
const App = ({ Component, pageProps }) => {
const { query } = useRouter()
const [theme, setTheme] = React.useState(defaultTheme)
React.useEffect(() => {
fetch(`/api/client?id=${query.clientId}`)
.then(res => res.json())
.then(client => {
theme.colors.primary = client.color
setTheme(theme)
})
}, [query.clientId])
return (
<ThemeProvider theme={theme}>
<Component {...pageProps} />
</ThemeProvider>
)
}
export default App This is using an imaginary API, but I hope will help point you in the right direction? :) |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
lachlanjc
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your exact implementation will depend a lot on the framework you're using & how you're architecting your app. However, if you wanted to do this in Next.js, here's how I might go about it: