In one React app with React Router, for each route component, I would like to have one local scss file only apply to this file.
For below example, about.js imported about.scss, what I want to have is: about.scss override the global style in app.scss only in about.js. However, it overrode everything in the app
app.js - imported app.scss which I prefer as a global style
import React from "react";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
import "./app.scss"; // global scss
import About from "./about";
export default function App() {
return (
<>
<h1>question about scss in react route</h1>
<Router>
<div>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
</ul>
<hr />
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
</div>
</Router>
</>
);
}
function Home() {
return (
<div>
<h3 className={"myMargin"}>home</h3>
</div>
);
}
app.scss (the global style)
h3.myMargin {
margin: 10px
}
about.js
import React from "react";
import "./about.scss";
export default function About() {
return (
<div>
<h3 className={"myMargin"}>About</h3>
</div>
);
}
about.scss (I want to be a local style, however, it applied to everywhere in the app)
h3.myMargin {
margin: 100px
}
Any suggestion how to structure my style code?