Skip to content

Dynamic generated theme from API #1577

Answered by lachlanjc
shuyang0129 asked this question in Q&A
Discussion options

You must be logged in to vote

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)
      

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@shuyang0129
Comment options

@hasparus
Comment options

@shuyang0129
Comment options

@lachlanjc
Comment options

@shuyang0129
Comment options

Answer selected by lachlanjc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants