Skip to content

Commit

Permalink
🔧 Context
Browse files Browse the repository at this point in the history
Context
  • Loading branch information
SaishJ committed Sep 12, 2022
1 parent d60d9be commit c925fec
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,6 @@ const UpdatedComponent = (OriginalComponent) => {
export default UpdatedComponent;
```

### [Render Peops]()
### [Render Peops](https://github.com/SaishJ/React-JS-Beginner-to-Advanced/commit/d60d9bec7747e75247a97d22406520cbac48fa0a)

### [Context]()
13 changes: 9 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,25 @@ import HoverCounter from "./components/HoverCounter";
import Increment from "./components/Increment";
import ClickCounterTwo from "./components/ClickCounterTwo";
import HoverCounterTwo from "./components/HoverCounterTwo";
import { UserProvider } from "./components/useContext";
import ComponentA from "./components/ComponentA";

function App() {
return (
<div className="App">
<Increment
<UserProvider value="Saish">
<ComponentA />
</UserProvider>
{/* <Increment
render={(count, incrementCount) => (
<ClickCounterTwo count={count} incrementCount={incrementCount} />
)}
/>
<Increment
/> */}
{/* <Increment
render={(count, incrementCount) => (
<HoverCounterTwo count={count} incrementCount={incrementCount} />
)}
/>
/> */}
{/* <ClickCounter name="Saish" /> */}
{/* <HoverCounter name="Pratik" /> */}
{/* <ErrorBoundary>
Expand Down
14 changes: 14 additions & 0 deletions src/components/ComponentA.js
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;
20 changes: 20 additions & 0 deletions src/components/ComponentB.js
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
22 changes: 22 additions & 0 deletions src/components/ComponentC.js
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;
13 changes: 13 additions & 0 deletions src/components/useContext.js
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.
*/

0 comments on commit c925fec

Please sign in to comment.