Skip to content

Instantly share code, notes, and snippets.

@pavsidhu
Last active May 14, 2019 13:15
Show Gist options
  • Save pavsidhu/dc9342f96a66ca4b67cedb429ad68a6f to your computer and use it in GitHub Desktop.
Save pavsidhu/dc9342f96a66ca4b67cedb429ad68a6f to your computer and use it in GitHub Desktop.
Redux action example
class App extends Component {
render() {
return (
// this.props.count comes from the Redux state
<Text>{this.props.count}</Text>
// This.props.addToCounter() is a function to update the counter
<Button onPress={() => this.props.addToCounter()}>
Click Me!
</Button>
)
}
}
// This function provides a means of sending actions so that data in the Redux store
// can be modified. In this example, calling this.props.addToCounter() will now dispatch
// (send) an action so that the reducer can update the Redux state.
function mapDispatchToProps(dispatch) {
return {
addToCounter: () => dispatch(addToCounter())
}
}
// This function provides access to data in the Redux state in the React component
// In this example, the value of this.props.count will now always have the same value
// As the count value in the Redux state
function mapStateToProps(state) {
return {
count: state.count
}
}
export default connect(mapStateToProps, mapDispatchToProps)(App)
@Selman555
Copy link

Selman555 commented Jun 23, 2017

Could you add your imports to make it less confusing?
Without the proper imports, addToCounter() is undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment