骚操作!在react中使用vuex
- createStore
const store = new Reux({
state: {
list: [{count: 1}]
},
mutations: {
add (state, payload) {
state.list.push(payload)
}
},
actions: {
add ({commit}, payload) {
setTimeout(function() {
commit('add', payload);
}, 1000);
}
}
})
- Provider
ReactDOM.render(
<Provider store = {store}>
<App />
</Provider>,
document.getElementById('root'));
- connect
class List extends Component {
render() {
const { list } = this.props;
return (
<ul>
{
list.map((i, idx) => <li key={idx}>{i.count}</li>)
}
</ul>
)
}
}
export default connect((state) => {
return {
list: state.list
}
})(List);
$ cd demos/todolist
$ npm install
$ npm start
- 支持Module