react如何请求数据异步


reactreact react1npm i redux-thunk –save npm i axios –savethunk2store.jsthunk3actionCreatorredux-thunkreactreact1saga er62promise3thunk4reduxactionactionCreatorthunk

npmiredux-thunk--save
npmiaxios--save

store.js

import{createStore,applyMiddleware}from'redux'//applyMiddleware
importreducersfrom'./reducers'//reducer
importthunkfrom'redux-thunk'//thunk
varstore=createStore(reducer,applyMiddleware(thunk));//
exportdefaultstore;

actionCreator

importaxiosfrom'axios'
exportdefault{
getData(){
return(dispatch)=>{//redux-thunk
axios.get('http://www.xmyxapp.com/api/tab/1?start=0').then((res)=>{
console.log(res)
dispatch({
type:'GETDATA',
list:res.data.data.items.list
})
})
}
}
}

reducer

varinitState={
list:[]
}
exportconstlistReducer=(state=initState,action)=>{
varnewState={...state};
swi免费云主机域名tch(action.type){
case'GETDATA':
newState.list=action.list//action.listactionCreator
returnnewState;
default:
returnstate
}
}

storereducers

import{combineReducers}from'redux'
import{listReducer}from'../components/list/reducer'
varreducer=combineReducers({
list:listReducer
})
exportdefaultreducer;

importReact,{Component}from'react'
import{connect}from'react-redux'
importactionCreatorfrom'./actionCreator';
classListextendsComponent{
componentDidMount(){
this.props.getData();//
}
render(){
console.log(this.props);
return(
    { this.props.list.list.map((item)=>{ returnitem.title?
  • {item.title}
  • :"" }) }
) } } exportdefaultconnect((state)=>state,actionCreator)(List);

reactreact

相关推荐: vue使用element ui弹窗与echarts间的问题如何解决

这篇文章主要介绍“vue使用element ui弹窗与echarts间的问题如何解决”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“vue使用element ui弹窗与echarts间的问题如何解决”文章能帮助大家解决问…

免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。

(0)
打赏 微信扫一扫 微信扫一扫
上一篇 03/02 17:28
下一篇 03/02 17:44

相关推荐