Skip to content

Instantly share code, notes, and snippets.

@mikker
Last active April 1, 2016 14:20
Show Gist options
  • Save mikker/ca71d52207c3fa2bf8b111820b72619b to your computer and use it in GitHub Desktop.
Save mikker/ca71d52207c3fa2bf8b111820b72619b to your computer and use it in GitHub Desktop.
import React, { Component, PropTypes } from 'react'
import { connect } from 'react-redux'
/* ACTIONS */
export const ADD = 'something_clever/ADD'
export function add () {
return { type: ADD }
}
/* REDUCER */
export const initialState = {
count: 0
}
export function reducer (state = initialState, action) {
switch(action.type) {
case ADD:
return { ...state, count: state.count + 1 }
default:
return state
}
}
/* COMPONENT */
const stateToProps = (state) => ({
count: state.something_clever.count
})
class SomethingClever extends Component {
static propTypes = {
count: PropTypes.number,
dispatch: PropTypes.func.isRequired
}
render () {
const { count, dispatch } = this.props
return <div className='SomethingClever'>
<p>{count}</p>
<button onClick={() => dispatch(add())}>+</button>
</div>
}
}
export default connect(stateToProps)(SomethingClever)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment