In a project, i have different components with different css imports and in the app i use react-router to define the paths. When I access /anycomponent i have their imports in the head, but all the css of the other components also go there. Is there any way to import a component's css only when it is rendered?
import React from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import component1 from './pages/component1';
import component2 from './pages/component2';
export default function Routes() {
return (
<BrowserRouter>
<Switch>
<Route path="/" exact component={component1} />
<Route path="/component2" component={component2} />
</Switch>
</BrowserRouter>
);
}