diff --git a/README.md b/README.md index 23e27df..2e52b43 100644 --- a/README.md +++ b/README.md @@ -219,3 +219,22 @@ export default ClassClick; ### [Forwarding Refs](https://github.com/SaishJ/React-JS-Beginner-to-Advanced/commit/9a57ffbe3d6eeea400c3604bc66fe6bb6bafabdd) ### [Portals](https://github.com/SaishJ/React-JS-Beginner-to-Advanced/commit/89a9384c40963c77ec47ac76cbd8699ee1bebe4d) + +### [Error Boundary](https://github.com/SaishJ/React-JS-Beginner-to-Advanced/commit/83c11fa8ef5b3c7cf3a0a71495993ea9fed97053) + +### [Higher Order Component]() + +```jsx +import React from "react"; + +const UpdatedComponent = (OriginalComponent) => { + class NewComponent extends React.Component { + render() { + return ; + } + } + return NewComponent; +}; + +export default UpdatedComponent; +``` diff --git a/src/App.js b/src/App.js index 27129c4..10e1434 100644 --- a/src/App.js +++ b/src/App.js @@ -24,11 +24,15 @@ import FRParentInput from "./components/FRParentInput"; import PortalDemo from "./components/PortalDemo"; import Hero from "./components/Hero"; import ErrorBoundary from "./components/ErrorBoundary"; +import ClickCounter from "./components/ClickCounter"; +import HoverCounter from "./components/HoverCounter"; function App() { return (
- + + + {/* @@ -36,7 +40,7 @@ function App() { - + */} {/* */} {/* */} {/* */} diff --git a/src/components/ClickCounter.js b/src/components/ClickCounter.js new file mode 100644 index 0000000..826ff69 --- /dev/null +++ b/src/components/ClickCounter.js @@ -0,0 +1,17 @@ +import React, { Component } from "react"; +import withCounter from "./withCounter"; + +class ClickCounter extends Component { + render() { + const { count, incrementCount, name } = this.props; + return ( +
+ +
+ ); + } +} + +export default withCounter(ClickCounter, 5); diff --git a/src/components/HoverCounter.js b/src/components/HoverCounter.js new file mode 100644 index 0000000..dbb774f --- /dev/null +++ b/src/components/HoverCounter.js @@ -0,0 +1,17 @@ +import React, { Component } from "react"; +import withCounter from "./withCounter"; + +class HoverCounter extends Component { + render() { + const { count, incrementCount, name } = this.props; + return ( +
+

+ {name} Hover {count} times +

+
+ ); + } +} + +export default withCounter(HoverCounter, 10); diff --git a/src/components/withCounter.js b/src/components/withCounter.js new file mode 100644 index 0000000..a1c74a6 --- /dev/null +++ b/src/components/withCounter.js @@ -0,0 +1,37 @@ +import React from "react"; + +const withCounter = (WrappedComponent, incrementNumber) => { + class WithCounter extends React.Component { + constructor(props) { + super(props); + + this.state = { + count: 0, + }; + } + + incrementCount = () => { + this.setState((prevState) => { + return { count: prevState.count + incrementNumber }; + }); + }; + + render() { + return ( + + ); + } + } + return WithCounter; +}; + +export default withCounter; + +/* +To share common functionality between components. +A Higher Order Components (HOC) pattern where a function takes a component as an argument and returns a new component. +*/