小编Pok*_*oud的帖子

React Styled-Components主题-提供者动态主题

我正在尝试在我的React应用程序中实现深色和浅色主题。我知道主题是如何工作的,因此我可以像下面这样配置按钮:

const Button = styled.button`
   /* some styles */
    color: ${props => props.theme.main}
`;
Run Code Online (Sandbox Code Playgroud)

然后我将主题定义为const:

const dark = {
    main: 'black',
    text: 'switch to light mode'
};

const light = {
    main: 'white',
    text: 'switch to dark mode'
};
Run Code Online (Sandbox Code Playgroud)

当我想在某个地方使用主题时,我会像这样:

  <ThemeProvider theme={dark}>
    <Button>{dark.text}</Button>
  </ThemeProvider>
Run Code Online (Sandbox Code Playgroud)

但是我要实现的是动态更改主题(可能在按钮的单击功能上)。我对React很陌生,所以请不要对我卑鄙。

javascript themes state reactjs styled-components

2
推荐指数
1
解决办法
1605
查看次数

标签 统计

javascript ×1

reactjs ×1

state ×1

styled-components ×1

themes ×1