Last active
May 5, 2020 06:40
-
-
Save anmolsukki/e6682426b9c8dd954c82deb04ea73abf to your computer and use it in GitHub Desktop.
[ ReactJs ] HOC ( https://codesandbox.io/s/hoc-kokuf )
This file contains hidden or 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
| import React from "react"; | |
| import Rahul from "./Components/Rahul"; | |
| import Rose from "./Components/Rose"; | |
| function App() { | |
| return ( | |
| <div className="App"> | |
| <Rahul camp="120" /> | |
| <Rose camp="140" /> | |
| </div> | |
| ); | |
| } | |
| export default App; |
This file contains hidden or 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
| // Component/Rahul.js | |
| import React, { Component } from "react"; | |
| import WithArms from "../Hoc/WithArms"; | |
| class Rahul extends Component { | |
| render() { | |
| return ( | |
| <div> | |
| <h2>camp: {this.props.camp}</h2> | |
| <h3 onMouseOver={this.props.hocHandleGunShots}> | |
| Rahul {this.props.hocGunName} GunShots: {this.props.hocGunShots} | |
| </h3> | |
| </div> | |
| ); | |
| } | |
| } | |
| export default WithArms(Rahul, 10); |
This file contains hidden or 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
| // Component/Rose.js | |
| import React, { Component } from "react"; | |
| import WithArms from "../Hoc/WithArms"; | |
| class Rose extends Component { | |
| render() { | |
| return ( | |
| <div> | |
| <h2>camp: {this.props.camp}</h2> | |
| <h3 onMouseOver={this.props.hocHandleGunShots}> | |
| Rahul {this.props.hocGunName} GunShots: {this.props.hocGunShots} | |
| </h3> | |
| </div> | |
| ); | |
| } | |
| } | |
| export default WithArms(Rose, 20); |
This file contains hidden or 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
| // Hoc/WithArms.js | |
| import React, { Component } from "react"; | |
| const WithArms = (Men, shot) => { | |
| class newArms extends Component { | |
| constructor() { | |
| super(); | |
| this.state = { | |
| gunShots: 0 | |
| }; | |
| } | |
| handleShots = () => { | |
| this.setState({ | |
| gunShots: this.state.gunShots + shot | |
| }); | |
| }; | |
| render() { | |
| return ( | |
| <div> | |
| <Men | |
| hocGunName="Ak47" | |
| hocGunShots={this.state.gunShots} | |
| hocHandleGunShots={this.handleShots} | |
| {...this.props} | |
| /> | |
| </div> | |
| ); | |
| } | |
| } | |
| return newArms; | |
| }; | |
| export default WithArms; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment