11.拆分state把请求api文章列表,数据渲染到页面上
const initState = {
num: 160,
list: []
}
// reducer
export const weight = (state = initState , action) => {
switch (action.type) {
case EAT:
return {...state, num: state.num + 10}
case HUNGRY:
return {...state, num: state.num - 10}
case ARTICLE_LIST:
console.log('action',action)
return {...state, list: action.playload.article}
default:
return initState
}
}
export const getArticle = () =>{
return dispatch =>{
setTimeout(()=>{
// dispatch(eat('煎饼'))
axios.get('/v1/article/list').then(res=>{
console.log('article',res)
//做了一个分发
dispatch({type:ARTICLE_LIST,playload:res.data})
})
},2000)
}
}
页面渲染
const {num, eat, hungry , eatAsync, getArticle, list} = this.props
render
{list.map((item,index)=>{
return <div key={index}>
{item.title}
</div>
})}
export default connect((state)=>{
return {num: state.num,list:state.list}
},{eat,hungry,eatAsync,getArticle})(App)