简体   繁体   English

Dispatch 不将操作发送到减速器

[英]Dispatch not sending the action to the reducer

I have a react redux app and I am having an issue with dispatching the action to the reducer.我有一个反应 redux 应用程序,我在将操作分派给减速器时遇到问题。 This is what I have in my actions.js这就是我的 actions.js

import GetDataFromAPIs from "../utils/api-data";
export const UPDATE_DATA = 'UPDATE_DATA';

export const updateData = async => {
    const newData = await GetDataFromAPIs();

    return{
        type: UPDATE_DATA, 
        payload: newData
    };
};

and this is how I am calling updateData from index.js:这就是我从 index.js 调用updateData的方式:

const menuStore = createStore(MyReducer, composeWithDevTools(applyMiddleware(thunk)));
menuStore.dispatch(applyMiddleware(thunk)(updateData));

I can step through the actions.js but I don't see menuStore.dispatch sending the action to the reducer.我可以单步执行actions.js,但我没有看到menuStore.dispatch 将操作发送到reducer。 What am I missing?我错过了什么?

When using thunk middleware you need to pass the 'thunk' to the dispatch function.使用 thunk 中间件时,您需要将“thunk”传递给调度 function。
First, the thunk need some modification.首先,thunk 需要一些修改。 As the docs say here正如文档所说here

A thunk is a function that wraps an expression to delay its evaluation thunk 是 function 包装表达式以延迟其评估

Therefore need to change the thunk as below.因此需要如下改变thunk。

export const updateData = async (dispatch)=> {
   const newData = await GetDataFromAPIs();

   dispatch({
      type: UPDATE_DATA, 
      payload: newData
    });
};

Now pass this thunk to the store.dispatch() function.现在将此 thunk 传递给store.dispatch() function。

menuStore.dispatch(updateData);
  • If using thunk, then your action needs to return a function.如果使用 thunk,那么您的操作需要返回 function。 The funtion will take dispatch as an argument which will be used to dispatch actions based on api response.该函数将 dispatch 作为参数,用于根据 api 响应调度操作。
  • While calling dispatch you need not use middleware.在调用调度时,您不需要使用中间件。
  • Also the action needs to be called in the dispatch menuStore.dispatch(updateData()) not just passing reference还需要在调度menuStore.dispatch(updateData())中调用该操作,而不仅仅是传递引用
export const updateData = inputData => async dispatch => {
    const newData = await GetDataFromAPIs();
    dispatch({
        type: UPDATE_DATA,
        payload: newData
    })
};

Usage用法

menuStore.dispatch(updateData())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Reactjs - 向 Reducer 发送调度问题? - Reactjs - Issue Sending Dispatch to Reducer? redux调度动作和减速器具有新状态 - redux dispatch action and reducer with new state React Redux 工具包 - 我们可以从一个 reducer 的操作调度/调用另一个 reducer 的操作来更改 state 变量 - React Redux Toolkit - Can we Dispatch/Call from one reducer's action to another reducer's action to change state variable 调度在reducer中的调度 - Schedule dispatch in reducer Dispatch 不是减速机中的 function - Dispatch is not a function in reducer 如何在将 createPost 操作发送到减速器后从后端 API 生成新创建的帖子 ID - How to get newly created post id generated from backend API after dispatch createPost action to a reducer 我的 class 组件将我的操作发送到减速器,但它在存储中显示 NULL 数组 - My class component dispatch my action to reducer,but it show NULL array in store 从一个动作文件中分派来自另一个reducer文件的动作是一种反模式吗? - Is it an anti pattern to dispatch actions from an action file that are subscribed to from a different reducer file? 如何使用Jest在异步调用中测试动作缩减器并进行调度? - How Can I test a an action reducer with an async call and dispatch inside using Jest? this.props.dispatch没有调用reducer - this.props.dispatch is not calling reducer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM