实际上,我不了解如何在样式化组件中使用{withTheme}及其用法。因此,任何人都可以在样式化组件中使用{withTheme}来提供适当的代码。
withTheme
帮助您从组件道具中获取主题变量。定义主题时,通常可以在样式化的组件中使用它们,但是如果使用withTheme
HOC定义组件,则可以在组件中使用这些变量。
// Define our button, with the use of props.theme
const Button = styled.button`
color: ${props => props.theme.fg};
border: 2px solid ${props => props.theme.fg};
background: ${props => props.theme.bg};
`;
// Define our button, but with the use of component.props
const ThemeButton = (props) => (
<button style={{background: props.theme.bg, color: props.theme.fg, border: `1px solid ${props.theme.fg}`}}>
{`this button is using: ${props.theme.fg} and ${props.theme.bg}`}
</button>
)
const ButtonWithTheme = withTheme(ThemeButton);
// Define our `fg` and `bg` on the theme
const theme = {
fg: "palevioletred",
bg: "white"
};
<ThemeProvider theme={theme}>
<div>
<Button>Default Theme</Button>
<ButtonWithTheme></ButtonWithTheme>
</div>
</ThemeProvider>
您可以在这里签入https://codesandbox.io/s/zealous-sky-kgjqj?fontsize=14
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句