Last active
August 31, 2017 18:19
-
-
Save smith/dfe913dce779b3a65bc226ea97d4254c to your computer and use it in GitHub Desktop.
Caching HOC?
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
import React, { Component } from 'react' | |
import cacheComponent from './cacheComponent' | |
class AComponent extends Component { | |
onClick = () => { | |
this.props.set({ clicked: true }); | |
} | |
render() { | |
return <div>Have I been clicked? {this.props.cache.clicked ? "yes :)" : "no :("} <button onClick={this.onClick}>click me</button></div>; | |
} | |
} | |
export default cacheComponent(AComponent); |
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
export default {}; |
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
import appState from './appState' | |
import React, { Component } from 'react' | |
export default function cacheComponent(WrappedComponent) { | |
return class CacheComponent extends Component { | |
constructor(props) { | |
super(props); | |
this.state = appState; | |
} | |
set = (newState) => { | |
this.setState(newState); | |
} | |
render() { | |
return <WrappedComponent cache={this.state} set={this.set} {...this.props} />; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment