Last active
October 11, 2015 12:43
-
-
Save jackcallister/144e9d9d7654672405f7 to your computer and use it in GitHub Desktop.
Nested Redux Updates
This file contains hidden or 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
export function updateProp(value, path) { | |
return { | |
type: 'UPDATE_PROP', | |
payload: { | |
path, | |
value, | |
} | |
} | |
} |
This file contains hidden or 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, PropTypes } from 'react' | |
export default class App extends Component { | |
static propTypes = { | |
value: PropTypes.string.isRequired, | |
updateProp: PropTypes.func.isRequired, | |
} | |
render() { | |
return ( | |
<input value={this.props.value} | |
onChange={(e) => { this.props.updateProp(e.target.value) }} /> | |
) | |
} | |
} |
This file contains hidden or 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 { connect } from 'react-redux' | |
import { | |
mapStateToProps, | |
mapDispatchToProps, | |
mergeProps | |
} from './appSelectors' | |
import App from './App' | |
class AppConnector extends Component { | |
render() { | |
return <App {...this.props} /> | |
} | |
} | |
export default connect(mapStateToProps, mapDispatchToProps, mergeProps)(AppConnector) |
This file contains hidden or 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 { | |
updateProp, | |
} from './Actions' | |
export function mapStateToProps(store) { | |
return { | |
value: store.data.value, | |
path: ['value'], | |
} | |
} | |
export function mapDispatchToProps(dispatch) { | |
return { | |
updateProp: (path) => { | |
return (value, extraPath = []) => { | |
dispatch(updateProp(value, [...path, ...extraPath])) | |
} | |
}, | |
} | |
} | |
export function mergeEditingProps(stateProps, dispatchProps, ownProps) { | |
return { | |
...ownProps, | |
...stateProps, | |
...dispatchProps, | |
updateProp: dispatchProps.updateProp(stateProps.path), | |
} | |
} |
This file contains hidden or 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 set from 'lodash/object/set' | |
import cloneDeep from 'lodash/lang/cloneDeep' | |
const initialState = { | |
value: 'Test' | |
} | |
export default function data(state = initialState, action) { | |
switch(action.type) { | |
case 'UPDATE_PROP': | |
return set( | |
cloneDeep(state), | |
action.payload.path, | |
action.payload.value | |
) | |
default: | |
return state | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment