-
-
Save limweb/600182148592727e8f86 to your computer and use it in GitHub Desktop.
Minimalist redux 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
import React, { Component } from 'react' | |
import { render } from 'react-dom' | |
import { createStore, bindActionCreators } from 'redux' | |
import { connect, Provider } from 'react-redux' | |
const increment = () => ({ type: 'increment' }) | |
const counter = (state = 0, action) => { | |
switch (action.type) { | |
case 'increment': | |
return state + 1 | |
default: | |
return state | |
} | |
} | |
const store = createStore(counter) | |
const mapState = counter => ({ counter }) | |
const mapDispatch = dispatch => bindActionCreators({ increment }, dispatch) | |
@connect(mapState, mapDispatch) | |
class Counter extends Component { | |
render = () => <h1 onClick={ this.props.increment }> | |
{ this.props.counter } | |
</h1> | |
} | |
render( | |
<Provider store={ store }> | |
<Counter /> | |
</Provider>, | |
document.getElementById('root')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment