Last active
January 29, 2016 09:27
-
-
Save josegl/5a6c1ca5e64ac25f4b61 to your computer and use it in GitHub Desktop.
working example
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
import React from 'react'; | |
export default ({ | |
items, fetchItems | |
}) => { | |
if(items.size === 0) | |
fetchItems(); | |
return (<div>{items}</div>); | |
}; |
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
import React, { Component } from 'react'; | |
import List from './List'; | |
import { connect } from 'react-redux'; | |
import { fetchItemsFromApi } from './actions'; | |
const getDescription = item => (a.getIn(['propLevel1','propLevel2'])); //item is an Immutable.js Map | |
const mapStateToProps = (state) => { | |
const { items } = state; // items is an Immutable.js List of Immutable.js Maps. | |
return {items: items.map(item => <ListItem description={getDescription(item) id={item.get('id')}}/>)}; | |
} | |
const mapDispatchToProps = (dispatch) => { | |
return { | |
fetchItems: () => { | |
dispatch(fetchItemsFromApi()); | |
} | |
} | |
} | |
export default connect( | |
mapStateToProps, | |
mapDispatchToProps | |
)(List); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Perhaps it is a better idea to refactor
fetchItemsFromApi
tofetchItemsFromApiIfNeeded
in order to dispatch always an action.