Skip to content

Instantly share code, notes, and snippets.

@na0x2c6
Created August 24, 2018 06:28
Show Gist options
  • Save na0x2c6/c9f494dcd22f2dbe42ec91683d45998c to your computer and use it in GitHub Desktop.
Save na0x2c6/c9f494dcd22f2dbe42ec91683d45998c to your computer and use it in GitHub Desktop.
function withSubscription(WrappedComponent, selectData) {
return class extends React.Component {
constructor(props) {
super(props)
this.handleChange = this.handleChange.bind(this)
this.state = {
data: selectData(DataSource, props)
}
}
componentDidMount() {
DataSource.addChangeListener(this.handleChange)
}
componentWillUnmount() {
DataSource.removeChangeListener(this.handleChange)
}
handleChange() {
this.setState({
data: selectData(DataSource, this.props)
})
}
render() {
return <WrappedComponent data={this.state.data} {...this.props} />
}
}
}
@na0x2c6
Copy link
Author

na0x2c6 commented Aug 24, 2018

このコードは Reactのドキュメントページ に書かれているコードと同様のものです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment