Component Import
Another way of using styles is to import the css directly into
a component. Now we need to store this CSS next to our code in the
src-directory.
File: src/resources/styles/App.css
.app-component {
padding: 2rem;
border: 1px solid #000;
}
File: src/App.tsx
import './resources/styles/App.css'
const App = () => {
const name = "Function Component"
return (
<div className="app-component">
<h1>{ name }</h1>
</div>
)
}
export default App;
Function Component
caution
- As you can see, applying a css class to an element, has a slight
syntax deviation. Instead of using
classon the element, you need to specifyclassNamesinceclassis (obviously) a reserved word in JavaScript. - Once imported, (because SPA) the stylesheet is loaded into the DOM and cascades into all subsequent components!