简体   繁体   English

如何在React JS中覆盖CSS模块

[英]How to override css modules in react js

I have 2 themes for my site. 我的网站有2个主题。 When I import a topic for the first time, it is imported, the second time it does not change. 当我第一次导入主题时,它是第二次导入,但它不变。 I need to change the topic override css. 我需要更改主题覆盖CSS。 How can I solve the problem? 我该如何解决这个问题?

import React from 'react';

import { Settings } from 'react-feather';
import Modal from 'react-responsive-modal';

export default class ContentConfig extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            openModal: false,
            checked: true
        };
    }

    openModal = () => {
        this.setState({openModal: true});
    }

    closeModal = () => {
        this.setState({openModal: false});
    }

    changeRadio = (bool) => {
        this.props.state(bool);
        this.setState({checked: bool});
    }

    render() {
        return(
            <div className="contentConfig">
                <div onClick={this.openModal} className="editContentIcon">
                    <Settings />
                    <p>Settings</p>
                </div>
                <Modal open={this.state.openModal} onClose={this.closeModal} center>
                    <form style={{pading: 20}}> 
                        <legend>Choose a color</legend>
                        <div className="fieldset">
                            <div className="colorsBox">
                                <label htmlFor="radio1" className="colors" style={{background: "#5dafe5"}}></label>
                                <input 
                                    id="radio1"
                                    type="radio" 
                                    name="colors" 
                                    onChange={() => this.changeRadio(true)} 
                                    checked={this.state.checked} 
                                />
                            </div>
                            <div className="colorsBox">
                                <label htmlFor="radio2" className="colors" style={{background: "#d94f5c"}}></label>
                                <input 
                                    id="radio2"
                                    type="radio" 
                                    name="colors" 
                                    onChange={() => this.changeRadio(false)} 
                                    checked={!this.state.checked} 
                                />
                            </div>
                        </div>
                    </form>
                </Modal>
            </div>
        );
    }   
}

This is a child component for changing themes, when I select a theme, it calls a function in the parent component. 这是一个用于更改主题的子组件,当我选择一个主题时,它将在父组件中调用一个函数。

import React, { Component } from 'react';
import './App.css';
import ContentConfig from './ContentConfig.js';

export default class App extends Component {
    constructor() {
        super();
    }

    changeStyles = (bool) => {
        if(bool) {
            import('./LigthTheme.css').then(() => console.log('imported ligth theme'));
        } else {
            import('./DarkTheme.css').then(() => console.log('imported dark theme'));
        }
    }

    render() {
            return (
                <div className="mainBox"> 
                    <div className="menu">
                        <h1>Devices</h1>
                        <ul className="links">

                        </ul>
                        <ContentConfig state={this.changeStyles} />
                    </div>
                </div>
            );
    }
}

When I change the theme for the first time in the browser, it shows what was added to the header. 当我第一次在浏览器中更改主题时,它会显示添加到标题的内容。 After the second time when changing the theme, nothing changes in the header. 第二次更改主题后,标题中没有任何变化。

I see 2 issues here because of which your change in CSS is not working. 我在这里看到2个问题,因此您对CSS的更改无法正常工作。

First 第一

Once you have imported particular theme, the new import should not have any impact. 导入特定主题后,新导入将不会产生任何影响。 Since both of the css files are imported now. 由于现在都导入了两个css文件。

I don't think react will unload the previously import css file. 我不认为React会卸载以前导入的CSS文件。 For example, if you first imported LightTheme and then DarkTheme, at this time both of these are imported in the cache. 例如,如果您先导入LightTheme,然后再导入DarkTheme,则这两个时间都将导入到缓存中。

Try creating a tag for style sheet and then changing the value dynamically. 尝试为样式表创建标签,然后动态更改值。 That should resolve the issue. 那应该解决问题。

Second 第二

You are not passing any parameter in the call of state={this.changeStyles} . 您没有在state={this.changeStyles}的调用中传递任何参数。

Try passing something like state = {this.changeStyles(this, false)} should result in DarkTheme. 尝试传递类似state = {this.changeStyles(this, false)}将导致DarkTheme。

Let me know the outcome. 让我知道结果。

This answer may help as well. 这个答案也可能有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM