|
1 | 1 | --- |
2 | 2 | title: useContext |
3 | 3 | --- |
4 | | -<Sandpack> |
5 | | - |
6 | | - |
7 | | -```js |
8 | | -console.log('strek') |
9 | | - |
10 | | -import { createContext, useContext, useState } from 'react'; |
11 | | - |
12 | | -const ThemeContext = createContext(null); |
13 | | - |
14 | | -export default function MyApp() { |
15 | | - const [theme, setTheme] = useState('light'); |
16 | | - return ( |
17 | | - <ThemeContext.Provider value={theme}> |
18 | | - <Form /> |
19 | | - <label> |
20 | | - <input |
21 | | - type="checkbox" |
22 | | - checked={theme === 'dark'} |
23 | | - onChange={(e) => { |
24 | | - setTheme(e.target.checked ? 'dark' : 'light') |
25 | | - }} |
26 | | - /> |
27 | | - Use dark mode |
28 | | - </label> |
29 | | - </ThemeContext.Provider> |
30 | | - ) |
31 | | -} |
32 | | - |
33 | | -function Form({ children }) { |
34 | | - return ( |
35 | | - <Panel title="Welcome"> |
36 | | - <Button>Sign up</Button> |
37 | | - <Button>Log in</Button> |
38 | | - </Panel> |
39 | | - ); |
40 | | -} |
41 | | - |
42 | | -function Panel({ title, children }) { |
43 | | - const theme = useContext(ThemeContext); |
44 | | - const className = 'panel-' + theme; |
45 | | - return ( |
46 | | - <section className={className}> |
47 | | - <h1>{title}</h1> |
48 | | - {children} |
49 | | - </section> |
50 | | - ) |
51 | | -} |
52 | | - |
53 | | -function Button({ children }) { |
54 | | - const theme = useContext(ThemeContext); |
55 | | - const className = 'button-' + theme; |
56 | | - return ( |
57 | | - <button className={className}> |
58 | | - {children} |
59 | | - </button> |
60 | | - ); |
61 | | -} |
62 | | -``` |
63 | | - |
64 | | -```css |
65 | | -.panel-light, |
66 | | -.panel-dark { |
67 | | - border: 1px solid black; |
68 | | - border-radius: 4px; |
69 | | - padding: 20px; |
70 | | - margin-bottom: 10px; |
71 | | -} |
72 | | -.panel-light { |
73 | | - color: #222; |
74 | | - background: #fff; |
75 | | -} |
76 | | - |
77 | | -.panel-dark { |
78 | | - color: #fff; |
79 | | - background: rgb(23, 32, 42); |
80 | | -} |
81 | | - |
82 | | -.button-light, |
83 | | -.button-dark { |
84 | | - border: 1px solid #777; |
85 | | - padding: 5px; |
86 | | - margin-right: 10px; |
87 | | - margin-top: 10px; |
88 | | -} |
89 | | - |
90 | | -.button-dark { |
91 | | - background: #222; |
92 | | - color: #fff; |
93 | | -} |
94 | | - |
95 | | -.button-light { |
96 | | - background: #fff; |
97 | | - color: #222; |
98 | | -} |
99 | | -``` |
100 | | - |
101 | | -</Sandpack> |
102 | 4 | <Intro> |
103 | 5 |
|
104 | 6 | `useContext` is a React Hook that lets you read and subscribe to [context](/learn/passing-data-deeply-with-context) from your component. |
@@ -280,9 +182,7 @@ In this example, the `MyApp` component holds a state variable which is then pass |
280 | 182 |
|
281 | 183 | <Sandpack> |
282 | 184 |
|
283 | | -
|
284 | 185 | ```js |
285 | | -console.log('strek') |
286 | 186 |
|
287 | 187 | import { createContext, useContext, useState } from 'react'; |
288 | 188 |
|
|
0 commit comments