File tree 5 files changed +142
-2
lines changed
5 files changed +142
-2
lines changed Original file line number Diff line number Diff line change 3
3
1. CSS Stylesheet Regular css stylesheet
4
4
2. Inline Styling
5
5
3. CSS Modules
6
- 4. CSS in js Libraries (Styled Components)
7
- > Here we will cover only first three approaches.
6
+ 4. Styled Components (CSS using Libraries)
7
+ > Install the package: npm install --save styled-components
8
+ > For Typescript: npm i --save-dev @types/styled-components
8
9
9
10
1. CSS Stylesheet (Regular css stylesheet)
10
11
> src/Components/Stylesheet.tsx
39
40
> Similar we have imported applyStyle.module.css file in parent component (App.tsx). But
40
41
We can not use it's classes in child component, because to use module.css we have to
41
42
import styles attribute then we can access it's classes.
43
+
44
+ 4. Styled Components
45
+ > Components/StyledComponent.tsx file has been created to utilize the styled component
46
+ styling. This file has been calling from the App.tsx.
42
47
43
48
44
49
Original file line number Diff line number Diff line change 13
13
"react" : " ^18.2.0" ,
14
14
"react-dom" : " ^18.2.0" ,
15
15
"react-scripts" : " 5.0.1" ,
16
+ "styled-components" : " ^5.3.5" ,
16
17
"typescript" : " ^4.7.4" ,
17
18
"web-vitals" : " ^2.1.4"
18
19
},
39
40
" last 1 firefox version" ,
40
41
" last 1 safari version"
41
42
]
43
+ },
44
+ "devDependencies" : {
45
+ "@types/styled-components" : " ^5.1.26"
42
46
}
43
47
}
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import logo from './logo.svg';
3
3
import './App.css' ;
4
4
import { Stylesheet } from './Components/Stylesheet' ;
5
5
import { Inline } from './Components/Inline' ;
6
+ import { StyledComponent } from './Components/StyledComponent' ;
6
7
import '../src/applyStyle.css'
7
8
import styles from '../src/applyStyle.module.css'
8
9
@@ -12,6 +13,7 @@ function App() {
12
13
< header className = "App-header" >
13
14
< Stylesheet primary = { true } />
14
15
< Inline />
16
+ < StyledComponent />
15
17
{ /* use class from the appStyles.css */ }
16
18
< h1 className = 'error' > Error</ h1 >
17
19
{ /* use class from the appStyles.module.css */ }
Original file line number Diff line number Diff line change
1
+ import * as React from 'react' ;
2
+ import styled from "styled-components" ;
3
+
4
+ // Styled component named StyledButton
5
+ const StyledButton = styled . button `
6
+ background-color: black;
7
+ font-size: 32px;
8
+ color: white;
9
+ ` ;
10
+
11
+ export const StyledComponent = ( ) => {
12
+ return (
13
+ < div >
14
+ { /* Use it like any other component. */ }
15
+ < StyledButton > Login </ StyledButton >
16
+ </ div >
17
+ )
18
+ }
You can’t perform that action at this time.
0 commit comments