Skip to content

Instantly share code, notes, and snippets.

@josegl
Last active January 29, 2016 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josegl/5a6c1ca5e64ac25f4b61 to your computer and use it in GitHub Desktop.
Save josegl/5a6c1ca5e64ac25f4b61 to your computer and use it in GitHub Desktop.
working example
import React from 'react';
export default ({
items, fetchItems
}) => {
if(items.size === 0)
fetchItems();
return (<div>{items}</div>);
};
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);
@josegl
Copy link
Author

josegl commented Jan 29, 2016

Perhaps it is a better idea to refactor fetchItemsFromApi to fetchItemsFromApiIfNeeded in order to dispatch always an action.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment