【发布时间】:2017-12-27 04:20:39
【问题描述】:
你能告诉我吗 如何使用 redux 在 react 中获取数据?
这是我的代码 https://codesandbox.io/s/lYMwLM591
import { UPDATED_STORIES } from './actions';
import axios from 'axios';
export default function getTopStories(){
return (dispatch) => {
axios.get('data.json').then( res => {
console.log(res ,"respone");
dispatch(getTopStoriesAysnc(res))
})
}
};
function getTopStoriesAysnc(response){
return {
type: UPDATED_STORIES,
payload: response
};
}
【问题讨论】:
-
你想什么时候加载数据?
-
在组件加载时我已经编写了我的代码请检查codesandbox.io/s/lYMwLM591
-
你有 redux-thunk 中间件吗?
-
不确定..:(请检查代码
-
对于一个非常简单的应用程序/小部件,在 componentDidMount 上调度加载操作通常是可以的。如果您需要更全功能的东西,我实际上编写了一个库,用于轻松绑定同构数据加载器以响应组件github.com/davnicwil/react-frontload
标签: reactjs react-native react-router react-redux redux-thunk