File tree Expand file tree Collapse file tree 4 files changed +44
-0
lines changed
react-typescript-demo/src Expand file tree Collapse file tree 4 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ import { Oscar } from './components/props/Oscar'
7
7
import { Button } from './components/props/Button'
8
8
import { Input } from './components/props/Input'
9
9
import { Container } from './components/props/Container'
10
+ import { ThemeContextProvider } from './components/context/ThemeContext'
11
+ import { Box } from './components/context/Box'
10
12
11
13
import './App.css'
12
14
@@ -48,6 +50,9 @@ function App() {
48
50
/>
49
51
< Input value = '' handleChange = { event => console . log ( event ) } />
50
52
< Container styles = { { border : '1px solid black' , padding : '1rem' } } />
53
+ < ThemeContextProvider >
54
+ < Box />
55
+ </ ThemeContextProvider >
51
56
</ div >
52
57
)
53
58
}
Original file line number Diff line number Diff line change
1
+ import { useContext } from 'react'
2
+ import { ThemeContext } from './ThemeContext'
3
+
4
+ export const Box = ( ) => {
5
+ const theme = useContext ( ThemeContext )
6
+ return (
7
+ < div
8
+ style = { {
9
+ backgroundColor : theme . primary . main ,
10
+ color : theme . primary . text
11
+ } } >
12
+ Theme context
13
+ </ div >
14
+ )
15
+ }
Original file line number Diff line number Diff line change
1
+ import React , { createContext } from 'react'
2
+ import { theme } from './theme'
3
+
4
+ type ThemeContextProviderProps = {
5
+ children : React . ReactNode
6
+ }
7
+
8
+ export const ThemeContext = createContext ( theme )
9
+
10
+ export const ThemeContextProvider = ( {
11
+ children
12
+ } : ThemeContextProviderProps ) => {
13
+ return < ThemeContext . Provider value = { theme } > { children } </ ThemeContext . Provider >
14
+ }
Original file line number Diff line number Diff line change
1
+ export const theme = {
2
+ primary : {
3
+ main : '#3f51b5' ,
4
+ text : '#fff'
5
+ } ,
6
+ secondary : {
7
+ main : '#f50057' ,
8
+ text : '#fff'
9
+ }
10
+ }
You can’t perform that action at this time.
0 commit comments