Last active
July 20, 2018 08:16
-
-
Save jermsam/f8f4e7660ff9d55ae75713af8545110c to your computer and use it in GitHub Desktop.
A render prop to resolve products as a cross-cutting concern
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
| // 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