Я нашел решение, которое сработало для меня. По предложению Basile, я использую библиотеку emotion css-in-js, в которой стили входят в стилизованные компоненты.emotion поставляется с механизмом создания тем для динамического стиля CSS и поддержания глобального стиля.
Компоненты в стиле css-in-js
const MButton = styled.button`
background-color: ${(props) => props.theme.colors.primarycolor};//using theme
border: 3px solid $color;
border-radius: 20px 20px 20px 20px;
-moz-border-radius: 20px 20px 20px 20px;
-webkit-border-radius: 20px 20px 20px 20px;
-ms-border-radius: 20px 20px 20px 20px;
-o-border-radius: 20px 20px 20px 20px;
`;
export default class index extends Component { ... your component
Для глобального моделирования и создания тем я использовал эмоцию theme, themeprovider,и глобальные компоненты.Я определяю свои темы в отдельном файле, импортирую его, применяю
const makeGlobalStyles = (theme) => css` any additional global styling not dependent on differing themes in theme`;
//supply styles to global component which manages global theme style state
const GlobalStyles = withTheme(({ theme }) => (
<Global styles={makeGlobalStyles(theme)} />
));
сохранить тему как состояние в главном компоненте приложения ...
this.state = {
viewState: "DARK",
theme: theme.DARK,//use a method to change these states to then change the theme styles used across the app
Затем предоставлю состояние темы в реальном времени для всехкомпоненты, которые его используют
render(){
const {viewState,theme} = this.state;
return(
<ThemeProvider theme={theme}>
<GlobalStyles />
any logic for your application, pass prop theme to components to dynamically style to the theme
<SomeComponent theme={theme}/>
<ThemeProvider/>
)}