sections/api/helpers/with-theme.mdx
import Code from 'components/Code' import Table, { Row, Column } from 'components/Table'
withThemePrefer
useThemehook: For function components, theuseThemehook provides a simpler API.withThemeis primarily useful for class components.
This is a higher order component factory to get the current theme from a ThemeProvider and
pass it to your component as a theme prop.
Returns the passed component inside a wrapper (higher order component).
The passed component will receive a theme prop with the current theme object.
import { withTheme } from 'styled-components'
class MyComponent extends React.Component {
render() {
console.log('Current theme: ', this.props.theme)
// ...
}
}
export default withTheme(MyComponent)
All styled components automatically receive the theme as a prop, so this is only necessary if you wish to access the theme for other reasons.