Skip to content

Instantly share code, notes, and snippets.

@BretCameron
Created June 19, 2019 16:59
Show Gist options
  • Save BretCameron/32cce952081c55a8ac2c5665cc66035e to your computer and use it in GitHub Desktop.
Save BretCameron/32cce952081c55a8ac2c5665cc66035e to your computer and use it in GitHub Desktop.
A simple React counter component that uses Redux
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