-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Context
- Loading branch information
Showing
6 changed files
with
81 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React, { Component } from "react"; | ||
import ComponentB from "./ComponentB"; | ||
|
||
class ComponentA extends Component { | ||
render() { | ||
return ( | ||
<div> | ||
<ComponentB /> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default ComponentA; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React, { Component } from "react"; | ||
import ComponentC from "./ComponentC"; | ||
import UserContext from "./useContext"; | ||
|
||
class ComponentB extends Component { | ||
static contextType = UserContext; | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<h1>Component B context {this.context}</h1> | ||
<ComponentC /> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default ComponentB; | ||
|
||
// Context Type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React, { Component } from "react"; | ||
import { UserConsumer } from "./useContext"; | ||
|
||
class ComponentC extends Component { | ||
render() { | ||
return ( | ||
<div> | ||
<UserConsumer> | ||
{(userName) => { | ||
return ( | ||
<div> | ||
<h1>Hello {userName}</h1> | ||
</div> | ||
); | ||
}} | ||
</UserConsumer> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default ComponentC; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from "react"; | ||
|
||
const UserContext = React.createContext(); | ||
|
||
const UserProvider = UserContext.Provider; | ||
const UserConsumer = UserContext.Consumer; | ||
|
||
export { UserProvider, UserConsumer }; | ||
export default UserContext; | ||
|
||
/* | ||
Context provides a way to pass data through the component tree without having to pass props down manuallly at every level. | ||
*/ |