Last active
May 14, 2019 13:15
-
-
Save pavsidhu/dc9342f96a66ca4b67cedb429ad68a6f to your computer and use it in GitHub Desktop.
Redux action example
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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you add your imports to make it less confusing?
Without the proper imports,
addToCounter()
is undefined