React: Dynamic import css

1k Views Asked by At

I am new to react. I want to import either "darkMode.css" or "lightMode.css" (to a class-based component) based on the value of props.

Imagine I have the below function (in the class based component):

cssName = () => (this.props.mode === "dark"? "darkMode.css":"lightMode.css")

Is there a way to import "darkMode.css" or "lightMode.css" using this function?

Thank you for helping!

1

There are 1 best solutions below

2
On
cssName = () => {
  if (this.props.mode === 'dark') {
    return import('darkmode.css').then((module) => 
      // whatever you want to do with module
    );
  }

  return import('lightMode.css').then((module) =>
    // whatever you want to do with module
  );
};