Skip to content

Instantly share code, notes, and snippets.

@hoangthai0302
Forked from markerikson/connectExample.js
Created March 9, 2018 16:35
Show Gist options
  • Select an option

  • Save hoangthai0302/7403a99bac9695053dfa9664e36d2979 to your computer and use it in GitHub Desktop.

Select an option

Save hoangthai0302/7403a99bac9695053dfa9664e36d2979 to your computer and use it in GitHub Desktop.
React-Redux connect example
import {action1, action2} from "myActions";
import {bindActionCreators} from "redux";
import {connect} from "react-redux";
const mapStateToProps = (state, ownProps) = {
return {
counter : state.counter,
someComponentValue : state.things[ownProps.someIdProp]
};
}
const mapDispatchToProps = (dispatch) => {
return {
// same effect
firstAction : () => dispatch(action1()),
secondAction : bindActionCreators(action2, dispatch)
}
};
const MyStatelessComponent = (props) => {
const {counter, someComponentValue} = props;
const {firstAction, secondAction} = props;
return (
<div>
<div>Counter: {counter}</div>
<div>Other value: {someComponentValue}</div>
<button onClick={firstAction}>First Button</button>
<button onClick={secondAction}>Second Button</button>
</div>
);
};
const WrappedComponent = connect(mapStateToProps, mapDispatchToProps)(MyStatelessComponent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment