@@ -7,42 +7,11 @@ import Cockpit from '../components/Cockpit/Cockpit.component';
77// import WithClass from '../hoc/WithClass/withClass.hoc';
88import withClass from '../hoc/WithClass/withClass.closureHOC' ;
99import Aux from '../hoc/Auxiliary/Auxiliary.hoc' ;
10+ import AuthContext from '../context/auth/auth.context' ;
1011
1112// Required Styles
1213import AppStyleClasses from './App.module.css' ;
1314
14- /**
15- * Assume that, for the <Person/> component, we want to add
16- * in a dummy authentication whose handler is written inside
17- * the <App/> component, and it is invoked from the <Cockpit/>
18- * component. The <Cockpit/> component receives the
19- * loginHandler() function's reference as a `prop` from App
20- * component.
21- *
22- * Now, the loginHandler() event handler, which is invoked from
23- * the <Cockpit/> component, will simply change some state
24- * object, let's say we have a new state variable called
25- * `authenticated`, which is initially false.
26- *
27- * Inside the loginHandler() function, we can call setState and
28- * set `authenticated` to true, and this loginHandler()'s
29- * reference is sent to the <Cockpit/> component as a property
30- * called `login`.
31- *
32- * Now, we also want to get the information whether every
33- * <Person/> component is authenticated or not. And since the
34- * state of the authentication status, which is `authenticated`
35- * is stored at <App/> component, and also, since the App
36- * component only has access to the <Persons/> component, we
37- * can send in the state value of `authenticated` as a property
38- * to the <Persons/> component (let's say `isAuthenticated`
39- * prop). Inside the <Persons/> component, we can pass the
40- * `isAuthenticated` property to each <Person/> as (say)
41- * `isAuth` property. And inside the <Person/> component, we
42- * can print information on each <Person/> whether it is
43- * "Authenticated" or "Not Authenticated".
44- */
45-
4615class App extends Component {
4716 constructor ( props ) {
4817 super ( props ) ;
@@ -115,17 +84,49 @@ class App extends Component {
11584 console . log ( "[App.js] rendering..." ) ;
11685 let persons = null ;
11786
87+ /**
88+ * Because of context object, we need not send information
89+ * on `state.authenticated` as a prop.
90+ */
91+
11892 if ( this . state . showPersons ) {
11993 persons = (
12094 < Persons
12195 persons = { this . state . persons }
12296 clicked = { this . deletePersonHandler }
12397 nameChanged = { this . nameChangedHandler }
124- isAuthenticated = { this . state . authenticated }
12598 />
12699 ) ;
127100 }
128101
102+ /**
103+ * Now, the imported <AuthContext/> component should wrap
104+ * all the components that would need to use the context
105+ * variables/functions in our app.
106+ *
107+ * In this case, the <Person/> and the <Cockpit/>
108+ * components need to use the context variable/functions,
109+ * and so, we'll wrap that part of the returned JSX from
110+ * this App component, inside <AuthContext.Provider/>
111+ * component. Because we're providing the context to the
112+ * components that are wrapped inside the <AuthContext/>
113+ * component.
114+ *
115+ * The <AuthContext.Provider/> component, takes in a
116+ * `value` prop, in which we pass in the new values to the
117+ * context, depending on what we're trying to provide the
118+ * context for. If the `value` prop isn't described, then
119+ * the default context's definition will be taken as the
120+ * value of the context.
121+ *
122+ * In this case, both <Cockpit/> and the <Persons/>
123+ * components can access the context object's variables or
124+ * functions. In case of <Persons/> component, since
125+ * <Person/> is the child component of <Persons/>
126+ * component, we can also use the context directly inside
127+ * the <Person/> component, which is what we want.
128+ */
129+
129130 return (
130131 < Aux >
131132 < button
@@ -137,20 +138,32 @@ class App extends Component {
137138 >
138139 { this . state . showCockpit ? "Remove Cockpit" : "Show Cockpit" }
139140 </ button >
140- {
141- this . state . showCockpit ?
141+ < AuthContext . Provider value = { {
142+ authenticated : this . state . authenticated ,
143+ login : this . loginHandler
144+ } } >
145+ { this . state . showCockpit ? (
142146 < Cockpit
143147 title = { this . props . title }
144148 showPersons = { this . state . showPersons }
145149 personsLength = { this . state . persons . length }
146150 clicked = { this . togglePersonsHandler }
147151 login = { this . loginHandler }
148- /> : null
149- }
150- { persons }
152+ />
153+ ) : null }
154+ { persons }
155+ </ AuthContext . Provider >
151156 </ Aux >
152157 ) ;
153158 }
154159}
155160
156161export default withClass ( App , AppStyleClasses . App ) ;
162+
163+ /**
164+ * The one thing that doesn't change when using the Context API
165+ * is that, we still maintain the info to be passed, inside the
166+ * state of the component, because, react will only re-render
167+ * when the state/props in some component down the component
168+ * tree changes.
169+ */
0 commit comments