Last active
April 8, 2018 02:31
-
-
Save nkbt/9195575f4278642f67bc2808ef5c6c32 to your computer and use it in GitHub Desktop.
ImmutableJS + Redux
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'; | |
import {List} from 'immutable'; | |
import {shouldComponentUpdate} from 'react-addons-pure-render-mixin'; | |
import {connect} from 'react-redux'; | |
import getFiltersData from './filtersData'; | |
import {filtersDataReady} from './reducer'; | |
const Filters = React.createClass({ | |
propTypes: { | |
getFiltersData: React.PropTypes.func.isRequired, | |
values: React.PropTypes.instanceOf(List) | |
}, | |
componentWillMount() { | |
this.props.getFiltersData(); | |
}, | |
shouldComponentUpdate, | |
render() { | |
const {values} = this.props; | |
// ... | |
} | |
}); | |
const mapStateToProps = ({ | |
filters | |
}) => ({ | |
values: filters.get('value') | |
}); | |
const mapDispatchToProps = dispatch => ({ | |
getFiltersData: () => getFiltersData() | |
.then(data => dispatch(filtersDataReady(data))) | |
}); | |
export default connect( | |
mapStateToProps, | |
mapDispatchToProps | |
)(Filters); |
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 {fromJS} from 'immutable'; | |
export default () => Promise.resolve(fromJS({ | |
section: { | |
}, | |
group: { | |
}, | |
value: [ | |
{name: 'x', value: 1} | |
] | |
})); |
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 {fromJS} from 'immutable'; | |
export const FILTERS_DATA_READY = 'FILTERS_DATA_READY'; | |
export const filtersDataReady = data => ({type: FILTERS_DATA_READY, data}); | |
const initialState = fromJS({ | |
section: {}, | |
group: {}, | |
value: [] | |
}); | |
export const filters = (state = initialState, action) => { | |
switch (action.type) { | |
case FILTERS_DATA_READY: | |
return state.merge(action.data); | |
default: | |
return state; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When creating store in the browser: