Skip to content

Instantly share code, notes, and snippets.

@alonbardavid
Created October 9, 2019 17:42
Show Gist options
  • Save alonbardavid/ef66a7229e33af2e25b7d102a3fcc0f0 to your computer and use it in GitHub Desktop.
Save alonbardavid/ef66a7229e33af2e25b7d102a3fcc0f0 to your computer and use it in GitHub Desktop.
Why use mobx gist 3
//resource-view.js
function ResourceView(props) {
const {loading,resource,error} = props;
return loading? <loader/>:<ResourceItem resource={resource}/>
}
const mapStateToProps = (state,ownProps) => ({
...state.resources[ownProps.resourceId]
})
export default connect(mapStateToProps)(ResourceView)
// resource-list.js
function ResourceList(props) {
const resourceIds = [1,2,3,4,5,6,7,8]; //Just for example sake, this would usually be taken from the store as well
const {requestResource,resources} = props;
return <div>
{resourceIds.map(id=><div key={id}>
{resources[id]?<ResourceView resourceId={id} />:
<button onClick={()=>requestResource(id)}>load</button>}
</div>)}
</div>
}
const mapStateToProps = (state,ownProps) => ({
resources:state.resources
})
const mapDispatchToProps = (dispatch,ownProps) =>({
requestResource:()=>requestResource(ownProps.resourceId)(dispatch)
})
export default connect(mapStateToProps,mapDispatchToProps)(ResourceView)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment