File tree Expand file tree Collapse file tree 2 files changed +54
-2
lines changed Expand file tree Collapse file tree 2 files changed +54
-2
lines changed Original file line number Diff line number Diff line change @@ -32,7 +32,10 @@ import "./index.css";
32
32
// import Index from "./advance_react/useReducer";
33
33
34
34
// ! /* propDrilling */
35
- import PropDrilling from "./advance_react/prop_drilling/prop_drilling" ;
35
+ // import PropDrilling from "./advance_react/prop_drilling/prop_drilling";
36
+
37
+ // ! /* ContextAPI/useContext */
38
+ import ContextAPI from "./advance_react/useContext/useContext" ;
36
39
37
40
function App ( ) {
38
41
return (
@@ -67,7 +70,10 @@ function App() {
67
70
{ /* <Index /> */ }
68
71
69
72
{ /* propDrilling */ }
70
- < PropDrilling />
73
+ { /* <PropDrilling /> */ }
74
+
75
+ { /* contextAPI/useContext */ }
76
+ < ContextAPI />
71
77
</ div >
72
78
</ React . Fragment >
73
79
) ;
Original file line number Diff line number Diff line change
1
+ import React , { useState , useContext } from "react" ;
2
+ import { data } from "../../data" ;
3
+
4
+ const PeopleContext = React . createContext ( ) ;
5
+
6
+ function ContextAPI ( ) {
7
+ const [ people , setPeople ] = useState ( data ) ;
8
+
9
+ const removeItem = ( id ) => {
10
+ setPeople ( ( people ) => {
11
+ return people . filter ( ( person ) => person . id !== id ) ;
12
+ } ) ;
13
+ } ;
14
+
15
+ return (
16
+ < PeopleContext . Provider value = { { removeItem, people } } >
17
+ < h1 > ContextAPI/useContext</ h1 >
18
+ < List people = { people } />
19
+ </ PeopleContext . Provider >
20
+ ) ;
21
+ }
22
+
23
+ function List ( { people } ) {
24
+ const peopleData = useContext ( PeopleContext ) ;
25
+ return (
26
+ < >
27
+ { peopleData . people . map ( ( person ) => {
28
+ return < SinglePerson key = { person . id } { ...person } /> ;
29
+ } ) }
30
+ </ >
31
+ ) ;
32
+ }
33
+
34
+ function SinglePerson ( { id, name } ) {
35
+ const { removeItem } = useContext ( PeopleContext ) ;
36
+ return (
37
+ < div className = "item" >
38
+ < p > { name } </ p >
39
+ < button type = "button" onClick = { ( ) => removeItem ( id ) } >
40
+ romove
41
+ </ button >
42
+ </ div >
43
+ ) ;
44
+ }
45
+
46
+ export default ContextAPI ;
You can’t perform that action at this time.
0 commit comments