Skip to content

Instantly share code, notes, and snippets.

@jermsam
Last active July 20, 2018 08:16
Show Gist options
  • Save jermsam/f8f4e7660ff9d55ae75713af8545110c to your computer and use it in GitHub Desktop.
Save jermsam/f8f4e7660ff9d55ae75713af8545110c to your computer and use it in GitHub Desktop.
A render prop to resolve products as a cross-cutting concern
// src/WithCrossCuttingConcerns.js
import React,{Component} from 'react'
import PropTypes from 'prop-types'
import client from './feathers'
class WithCrossCuttingConcerns extends Component{
state={
products:[],
}
componentDidMount(){
this.fetchFromRemote()
client.service('products').on('created',()=>this.fetchFromRemote());
}
fetchFromRemote =()=>client.service('products').find({
query: {
$sort: {
price: -1 // sort them by price descending
}
}
}).then(
res=>this.setState({products:res.data})
)
render(){
const {render,} =this.props;
const{products}=this.state
return (
<div>
{render({products})}
</div>
)
}
}
WithCrossCuttingConcerns.propTypes={
render:PropTypes.func.isRequired,
}
export default WithCrossCuttingConcerns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment