Last active
September 4, 2016 17:52
-
-
Save dabbott/5043c36758ec67a43c6555d08057dff8 to your computer and use it in GitHub Desktop.
React Project Styleguide
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
export * as testActions, { at as testConstants } from './testActions' |
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
export const at = { | |
MY_FIRST_ACTION: 'MY_FIRST_ACTION', | |
MY_SECOND_ACTION: 'MY_SECOND_ACTION', | |
} | |
export const first = (item) => async (dispatch) => { | |
dispatch({type: at.MY_FIRST_ACTION, payload: item}) | |
return item | |
} | |
export const second = (url) => async (dispatch) => { | |
const result = await http.get(url) | |
dispatch({type: at.MY_SECOND_ACTION, payload: result}) | |
return result | |
} |
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, { Component } from 'react' | |
import { connect } from 'react-redux' | |
import { bindActionCreators } from 'redux' | |
import { createSelector } from 'reselect' | |
import { testActions } from '../actions' | |
const mapStateToProps = createSelector( | |
(state) => state.app, | |
(state) => state.ui.val1, | |
(state) => state.ui.val2, | |
(app, uiVal1, uiVal2) => ({ | |
xolo: app.xolo, | |
yolo: uiVal1 + uiVal2, | |
}) | |
) | |
const mapDispatchToProps = (dispatch) => ({ | |
testActions: bindActionCreators(testActions, dispatch), | |
}) | |
class TestContainer extends Component { | |
// propTypes come before defaultProps! | |
static propTypes = {} | |
static defaultProps = {} | |
state = {} | |
doStuff = (stuff) => { | |
this.setState({stuff}) | |
} | |
doAction = () => { | |
const {yolo} = this.props | |
testActions.first(yolo) | |
} | |
render() { | |
return ( | |
<div | |
onClick={this.doStuff} | |
onDblClick={this.doAction} | |
/> | |
) | |
} | |
} | |
export default connect(mapStateToProps, mapDispatchToProps)(TestContainer) |
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 { testConstants as c } from '../actions' | |
const initialState = { | |
item: null, | |
result: null, | |
} | |
export default (state = initialState, action) => { | |
const {type, payload} = action | |
switch (type) { | |
case c.MY_FIRST_ACTION: { | |
return {...state, item: payload} | |
} | |
case c.MY_SECOND_ACTION: { | |
return {...state, result: payload} | |
} | |
default: { | |
return state | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment