Skip to content

Instantly share code, notes, and snippets.

@sbycrosz
Last active March 8, 2018 09:13
Show Gist options
  • Save sbycrosz/06a0776b421a7759d44a5d82d9656c7c to your computer and use it in GitHub Desktop.
Save sbycrosz/06a0776b421a7759d44a5d82d9656c7c to your computer and use it in GitHub Desktop.
SimpleReducer_ConnectedComponent.js
// api/selectors.js
import _ from 'lodash';
export const createLoadingSelector = (actions) => (state) => {
// returns true only when all actions is not loading
return _(actions)
.some((action) => _.get(state, `api.loading.${action}`));
};
// components/todos/index.js
import { connect } from 'react-redux';
import Todos from './Todos';
import { createLoadingSelector } from '../../redux/api/selectors';
// Show loading on GET_TODOS_REQUEST
const loadingSelector = createLoadingSelector(['GET_TODOS']);
const mapStateToProps = (state) => ({ isFetching: loadingSelector(state) });
export default connect(mapStateToProps)(Todos);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment