简体   繁体   English

如何动态更改导入的 css 文件

[英]How can I dynmically change the imported css file

so I am working on this project and I am using next js (react) and a certain css package that is really helpful and it has two themes Dark mode and Light mode.所以我正在做这个项目,我正在使用下一个 js(反应)和某个 css package 这真的很有帮助,它有两个主题暗模式和亮模式。 but they are both in separate css files meaning that if I want to use the light mode I have to import it this way:但它们都在单独的 css 文件中,这意味着如果我想使用灯光模式,我必须以这种方式导入它:

import '-packagename/light-something.css'

and for Dark mode:对于深色模式:

import '-packagename/dark-something.css'

my question here is if there a possibility to have a certain button event to dynamically change from dark to light mode, like a theme button.我的问题是是否有可能让某个按钮事件从暗模式动态更改为亮模式,例如主题按钮。

Following is way to do conditional rendering as per your case以下是根据您的情况进行条件渲染的方法

if (condition) {
    import('lightcss')
   //incase of any module you could use it in then block
   //it's irrelevant  in ur case
    .then(('Menu') => {
        Menu.open();
    })
    .catch(error => {
     import("darkcss")
}) ;
}

I did something like what you want:我做了你想要的事情:

'./myImports.js': './myImports.js':

const myImport = (option) => {

        switch (option) {
                case 'a': return require('./myCSSFile.css');
                case 'b': return require('./othercss.css');
                default: return require('./myCSSFile.css');
        }
}

export default myImport;

'./mycomponent1.js': './mycomponent1.js':

import myImport from './myImports';

myImport('a');

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

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