Created
October 9, 2019 17:45
-
-
Save alonbardavid/fd0fc6f9b0dca641608e8dbe54c1615b to your computer and use it in GitHub Desktop.
Why use mobx gist6
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
//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