Created
June 19, 2019 16:59
-
-
Save BretCameron/32cce952081c55a8ac2c5665cc66035e to your computer and use it in GitHub Desktop.
A simple React counter component that uses Redux
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'; | |
const containerStyle = { | |
display: 'flex' | |
} | |
const buttonStyle = { | |
fontSize: '1.5rem', | |
width: '40px', | |
height: '40px' | |
} | |
class Counter extends Component { | |
addOne = () => { | |
this.props.dispatch({ type: 'ADD_ONE' }); | |
} | |
minusOne = () => { | |
this.props.dispatch({ type: 'MINUS_ONE' }); | |
} | |
render() { | |
return ( | |
<div className="App" > | |
<header className="App-header"> | |
<h1>{this.props.number}</h1> | |
<div style={containerStyle}> | |
<button onClick={this.minusOne} type="button" style={buttonStyle}>-</button> | |
<button onClick={this.addOne} type="button" style={buttonStyle}>+</button> | |
</div> | |
</header> | |
</div> | |
); | |
} | |
} | |
const mapStateToProps = (state) => { | |
return { | |
number: state.number | |
}; | |
} | |
export default connect(mapStateToProps)(Counter); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment