use-theme
TypeScript icon, indicating that this package has built-in type declarations

0.1.5 • Public • Published

Build Status

Install

# With NPM 
$ npm install use-theme
 
# With Yarn 
$ yarn add use-theme

Usage

import React, { useCallback } from 'react'
import { useTheme, ThemeProvider } from 'use-theme'
 
const ChangeThemeButton = ({ children, theme }) => {
  const [currentTheme, setTheme] = useTheme()
  const changeTheme = useCallback(() => setTheme(theme), [theme, setTheme])
  return (
    <button
      className={theme === currentTheme ? 'active' : ''}
      onClick={changeTheme}
    >
      {children}
    </button>
  )
}
 
const App = () => {
  const [theme, setTheme] = useTheme()
  return (
    <div className={theme}>
      <ChangeThemeButton theme="dark">Dark theme</ChangeThemeButton>
      <ChangeThemeButton theme="light">Light theme</ChangeThemeButton>
    </div>
  )
}
 
ReactDOM.render(
  <ThemeProvider>
    <App />
  </ThemeProvider>,
  document.getElementById('app')
)

Readme

Keywords

none

Package Sidebar

Install

npm i use-theme

Weekly Downloads

1

Version

0.1.5

License

MIT

Unpacked Size

24.6 MB

Total Files

56

Last publish

Collaborators

  • scastiel