Last active
February 19, 2016 10:58
-
-
Save namelos/94b522c4c9394f9b4b1d to your computer and use it in GitHub Desktop.
Minimalist redux example
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 { 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
能写个中间件 / 异步方面的小样吗?