Skip to content

Instantly share code, notes, and snippets.

@farism
Created October 9, 2016 18:53
Show Gist options
  • Save farism/61fbb6fe809147c7f550984bf88f086a to your computer and use it in GitHub Desktop.
Save farism/61fbb6fe809147c7f550984bf88f086a to your computer and use it in GitHub Desktop.
redux-fractal + redux-modules
// Pagination/module.js
import { createModule } from 'redux-modules';
export default createModule({
name: 'pagination',
initialState: {
currentPage: 0,
perPage: 50,
},
transformations: [{
type: 'SET_CURRENT_PAGE',
reducer: (state, { payload }) => ({ ...state, currentPage: payload }),
}, {
type: 'SET_PER_PAGE',
reducer: (state, { payload }) => ({ ...state, perPage: payload }),
}],
});
// Pagination/Uncontrolled.jsx
import React from 'react';
import { createStore } from 'redux';
import { local } from 'redux-fractal';
import R from 'ramda';
import Pagination from './Pagination';
import { actions, reducer } from './module';
const Uncontrolled = (props) => {
return (
<Pagination {...{
...props,
onChangePage: (page) => {
props.setCurrentPage(page);
props.onChangePage(page);
},
onChangePerPage: (perPage) => {
props.setPerPage(perPage);
props.onChangePerPage(perPage);
},
}} />
);
};
Uncontrolled.propTypes = { ...Pagination.propTypes };
Uncontrolled.defaultProps = { ...Pagination.defaultProps };
export default local({
key: (props) => props.localKey,
createStore: () => createStore(reducer),
mapDispatchToProps: (dispatch) => R.map(fn => payload => dispatch(fn(payload)), actions)
})(Uncontrolled);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment