Skip to content

Instantly share code, notes, and snippets.

@akinogu
Last active October 14, 2021 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akinogu/f3ebf51594dea3eba5beb48f74c97da6 to your computer and use it in GitHub Desktop.
Save akinogu/f3ebf51594dea3eba5beb48f74c97da6 to your computer and use it in GitHub Desktop.
[styled-components]theme
import { createContext, useContext, useState } from 'react'
import { ThemeProvider as StyledThemeProvider } from 'styled-components'
const ThemeContext = createContext({
setColor: (_: string) => {}
})
export const ThemeProvider: React.FC = ({ children }) => {
// デフォルト値としてblueを指定
const [color, setColor] = useState('blue')
return (
// スタイルを上書きするsetterをvalueにわたす
<ThemeContext.Provider value={{ setColor }}>
<StyledThemeProvider
theme={{ color }}
>
{children}
</StyledThemeProvider>
</ThemeContext.Provider>
)
}
export const useTheme = () => useContext(ThemeContext)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment