标签:ase div 异步 回顾 容器 就是 header xtend path
cnpm i redux react-redux redux-thunk -S
store/index.js
src/index.js
src/views/home/index.jsx + UI.jsx
views/home/reducer.js
const reducer = (state = {
bannerlist: [],
prolist: []
}, action) => {
const { type, data } = action;
switch (type) {
case 'CHANGE_BANNER_LIST':
return { ...state, ...{ bannerlist: data } };
case 'CHANGE_PRO_LIST':
return { ...state, ...{ prolist: data } }
default:
return state
}
}
export default reducer;
views/kind/reducer.js + views/cart/reducer.js
import { createStore, combineReducers } from 'redux'
import home from '@/views/home/reducer';
import kind from '@/views/kind/reducer';
import cart from '@/views/cart/reducer';
// combineReducers 合并reducer
const reducer = combineReducers({
home,
kind,
cart
})
const store = createStore(reducer);
export default store;
import store from './store';
function render () {
ReactDOM.render(
<Router>
<Switch>
<Route path="/" component = { App } />
</Switch>
</Router>,
document.getElementById('root')
)
}
render()
store.subscribe(render)
// 触发状态改变
store.dispatch({type: '', data: ''})
// 获取状态
store.getState() // {home: {}, kind: {}, cart: {}}
import { connect } from 'react-redux';
import UI from './UI';
export default connect()(UI)
import React, { Component } from 'react';
export default class extends Component {
componentDidMount () {
}
render () {
return (
<div className="box">
<header className = "header">首页</header>
<div className = "content">
首页
</div>
</div>
)
}
}
const reducer = (state = {
bannerlist: [],
prolist: []
}, action) => {
const { type, data } = action;
switch (type) {
case 'CHANGE_BANNER_LIST':
return { ...state, ...{ bannerlist: data } };
case 'CHANGE_PRO_LIST':
return { ...state, ...{ prolist: data } }
default:
return state
}
}
export default reducer;
import { createStore, combineReducers } from 'redux'
import home from '@/views/home/reducer';
import kind from '@/views/kind/reducer';
import cart from '@/views/cart/reducer';
const reducer = combineReducers({
home,
kind,
cart
})
const store = createStore(reducer);
export default store;
import store from './store';
import { Provider } from 'react-redux';
ReactDOM.render(
<Provider store={ store }>
<Router>
<Switch>
<Route path="/" component = { App } />
</Switch>
</Router>
</Provider>,
document.getElementById('root')
)
import { connect } from 'react-redux';
import UI from './UI';
// const mapStateToProps = ({ home: { bannerlist, prolist } }) => ({ bannerlist, prolist })
const mapStateToProps = (state) => {
return {
bannerlist: state.home.bannerlist,
prolist: state.home.prolist
}
}
export default connect(mapStateToProps)(UI)
import { getBannerlist, getProlist } from '@/utils/api';
const mapDispatchToProps = (dispatch) => {
return {
getBannerlist () {
getBannerlist().then(data => { dispatch({ type: 'CHANGE_BANNER_LIST', data: data.data } ) })
},
getProlist () {
getProlist().then(data => { dispatch({ type: 'CHANGE_PRO_LIST', data: data.data } ) })
}
}
}
export default connect(mapStateToProps, mapDispatchToProps)(UI)
import React, { Component } from 'react';
export default class extends Component {
componentDidMount () {
this.props.getBannerlist();
this.props.getProlist();
}
render () {
let { bannerlist, prolist } = this.props
return (
<div className="box">
<header className = "header">首页</header>
<div className = "content">
首页
{
bannerlist.map((item) => (
<p key={ item.bannerid }> { item.img } </p>
))
}
{
prolist.map((item) => (
<p key={ item.proid }> { item.proname } </p>
))
}
</div>
</div>
)
}
}
目前异步操作在容器组件内部,需要把异步操作抽离出来 ---- actionCreator.js
以首页为例
i
import { createStore, combineReducers, applyMiddleware } from 'redux'
import thunk from 'redux-thunk';//+++++
import home from '@/views/home/reducer';
import kind from '@/views/kind/reducer';
import cart from '@/vews/cart/reducer';
const reducer = combineReducers({
home,
kind,
cart
})
const store = createStore(reducer, applyMiddleware(thunk));//+++++
export default store;
import { getBannerlist, getProlist } from '@/utils/api';
export default {
getBannerlist (dispatch) {
getBannerlist().then(data => {
dispatch({
type: 'CHANGE_BANNER_LIST',
data: data.data
})
})
},
getProlist (dispatch) {
getProlist().then(data => {
dispatch({
type: 'CHANGE_PRO_LIST',
data: data.data
})
})
}
}
标签:ase div 异步 回顾 容器 就是 header xtend path
原文地址:https://www.cnblogs.com/hy96/p/11900579.html