[英]undefined data from my action creator ? React/redux/thunk
I'm trying to make an async action using Redux-thunk. 我正在尝试使用Redux-thunk进行异步操作。 I'm still don't really understanding how Async call works with Redux-Thunk but I'm beginning to get a few things. 我仍然不太真正了解Async调用如何与Redux-Thunk一起使用,但是我开始了解到一些东西。 I know that : 我知道 :
To start slowly I just want to pass some data into my redux-store-state. 要慢慢开始,我只想将一些数据传递到我的redux-store-state中。 So I can check it through the react dev tool. 所以我可以通过react dev工具检查它。
My Action-Creator and my Actions : 我的动作创建者和我的动作:
function loadData(dispatch) {
return axios.get(`http://localhost:3000/authors`)
.then(res => res.json())
.then(
data => dispatch({ type: 'LOAD_DATA_SUCCESS', data }),
err => dispatch({ type: 'LOAD_DATA_FAILURE', err })
);
}
export function loadData(data) {
return {
type: LOAD_DATA_SUCCESS,
data
}
}
export function failData(data) {
return {
type: LOAD_DATA_FAILURE,
err
}
}
To make sure that my local URL is sending the data correctly with axios I've also done this : 为了确保我的本地URL使用axios正确发送数据,我还这样做了:
export function fetchData() {
axios.get('http://localhost:3000/authors')
.then(function (response) {
console.log(response);
})
}
My reducer : 我的减速器:
const ThunkData = (state = {}, action) => {
switch (action.type) {
case 'LOAD_DATA_SUCCESS':
return Object.assign({}, state, {
data: action.data,
});
case 'LOAD_DATA_FAILURE':
return action.err
default:
return state;
}
};
My client.js where I'm dispatching my Action-Creator with store.dispatch(loadData());
我使用store.dispatch(loadData());
在其中分发Action-Creator的client.js store.dispatch(loadData());
: :
const loggerMiddleware = createLogger();
function configureStore(preloadedState) {
return createStore(
todoApp,
preloadedState,
applyMiddleware(
thunkMiddleware,
loggerMiddleware
), window.devToolsExtension ? window.devToolsExtension() : f => f
)
}
const store = configureStore()
store.dispatch(loadData());
render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('test')
);
When I launch my app I have a ThunkData
object in my store with a data
object but undefined. 启动应用程序时,我的商店中有一个ThunkData
对象,带有一个data
对象,但未定义。 I'm guessing there is something wrong on how my action creator is passing the data and how my reducer is receiving it. 我猜想我的动作创建者如何传递数据以及我的简化器如何接收数据有问题。
Thanks. 谢谢。
export function loadData(data) {
return {
type: LOAD_DATA_SUCCESS,
data
}
}
export function failData(data) {
return {
type: LOAD_DATA_FAILURE,
err
}
}
These two functions are redundant in your action file. 这两个功能在您的操作文件中是多余的。 Remove them and add an export to the first loadData function. 删除它们并将导出添加到第一个loadData函数。 Rest of the code is fine. 其余代码很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.