简体   繁体   English

来自动作创建者的未定义数据? React / Redux / Thunk

[英]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 : 我知道 :

  1. I need an Action-Creator 我需要一个行动创造者
  2. Some actions related to my Action-creator (something like "I'm calling the data", "I'm waiting for then", "Got it", "Oups there is an error") 与我的动作创建者相关的一些动作(例如“我正在呼叫数据”,“我正在等待数据”,“知道了”,“糟糕!”)
  3. Not sure about this but I need a Reducer to handle the action dispatched by my Action-Creator. 对此不确定,但我需要一个减速器来处理我的动作创建者分配的动作。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM