Skip to content

Commit

Permalink
🔀 Render Props
Browse files Browse the repository at this point in the history
Render Props
  • Loading branch information
SaishJ committed Sep 12, 2022
1 parent 1d7a901 commit d60d9be
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export default ClassClick;

### [Error Boundary](https://github.com/SaishJ/React-JS-Beginner-to-Advanced/commit/83c11fa8ef5b3c7cf3a0a71495993ea9fed97053)

### [Higher Order Component]()
### [Higher Order Component](https://github.com/SaishJ/React-JS-Beginner-to-Advanced/commit/1d7a9019dce79ea77fd420d25f5cd409392fb84d)

```jsx
import React from "react";
Expand All @@ -238,3 +238,5 @@ const UpdatedComponent = (OriginalComponent) => {

export default UpdatedComponent;
```

### [Render Peops]()
17 changes: 15 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,25 @@ import Hero from "./components/Hero";
import ErrorBoundary from "./components/ErrorBoundary";
import ClickCounter from "./components/ClickCounter";
import HoverCounter from "./components/HoverCounter";
import Increment from "./components/Increment";
import ClickCounterTwo from "./components/ClickCounterTwo";
import HoverCounterTwo from "./components/HoverCounterTwo";

function App() {
return (
<div className="App">
<ClickCounter name="Saish" />
<HoverCounter name="Pratik" />
<Increment
render={(count, incrementCount) => (
<ClickCounterTwo count={count} incrementCount={incrementCount} />
)}
/>
<Increment
render={(count, incrementCount) => (
<HoverCounterTwo count={count} incrementCount={incrementCount} />
)}
/>
{/* <ClickCounter name="Saish" /> */}
{/* <HoverCounter name="Pratik" /> */}
{/* <ErrorBoundary>
<Hero heroName="Superman" />
</ErrorBoundary>
Expand Down
14 changes: 14 additions & 0 deletions src/components/ClickCounterTwo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React, { Component } from "react";

class ClickCounterTwo extends Component {
render() {
const { count, incrementCount } = this.props;
return (
<div>
<button onClick={incrementCount}>Clicked {count} times</button>
</div>
);
}
}

export default ClickCounterTwo;
14 changes: 14 additions & 0 deletions src/components/HoverCounterTwo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React, { Component } from "react";

class HoverCounterTwo extends Component {
render() {
const { count, incrementCount } = this.props;
return (
<div>
<h1 onMouseOver={incrementCount}>Hover {count} times</h1>
</div>
);
}
}

export default HoverCounterTwo;
30 changes: 30 additions & 0 deletions src/components/Increment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { Component } from "react";

class Increment extends Component {
constructor(props) {
super(props);

this.state = {
count: 0,
};
}

incrementCount = () => {
this.setState((prevState) => {
return { count: prevState.count + 1 };
});
};

render() {
return (
<div>{this.props.render(this.state.count, this.incrementCount)}</div>
);
}
}

export default Increment;

/*
Sharing common functionality between components.
The term "render prop" refers to a technique for sharing code between React components using a prop whosevalue is a function.
*/

0 comments on commit d60d9be

Please sign in to comment.