【问题标题】:Flow typing errors when dispatching actions调度操作时的流输入错误
【发布时间】:2019-10-29 22:02:30
【问题描述】:

我在减少流输入错误方面取得了一些进展,但仍然停留在其中一组错误上。以下是我的 Actions 文件中的一些示例代码:

export const ADD_ERROR: 'ADD_ERROR' = 'ADD_ERROR';
export const CLEAR_ERRORS: 'CLEAR_ERRORS' = 'CLEAR_ERRORS';
export const TOGGLE_PROCESSING: 'TOGGLE_PROCESSING' = 'TOGGLE_PROCESSING';
export const RESET_STATE: 'RESET_STATE' = 'RESET_STATE';
export const CLEANUP_INPUT_COMPANY_INFO: 'CLEANUP_INPUT_COMPANY_INFO' = 'CLEANUP_INPUT_COMPANY_INFO';

type AddErrorAction = {type: typeof ADD_ERROR, payload: ValidationError};
type ClearErrorsAction = {type: typeof CLEAR_ERRORS};
type ToggleProcessingAction = {type: typeof TOGGLE_PROCESSING};
type ResetStateAction = {type: typeof RESET_STATE, userRole: Role | ''};
type CleanupInputCompanyInfoAction = {type: typeof CLEANUP_INPUT_COMPANY_INFO};

export type CustomerAction = 
     | AddErrorAction
     | ClearErrorsAction
     | ToggleProcessingAction
     | ResetStateAction
     | CleanupInputCompanyInfoAction;

这里是伴随的 Reducers 文件(为清楚起见进行了删节):

import { ADD_ERROR,
         CLEAR_ERRORS,
         TOGGLE_PROCESSING
         RESET_STATE,
         CLEANUP_INPUT_COMPANY_INFO } from '../actions/Customer';

export const customerReducer = (state: CustomerState, action: CustomerAction) => {
  switch (action.type) {
    case ADD_ERROR: {
      return {
        // Code here
      };
    }

    case CLEAR_ERRORS: {
      return {
        // Code here
      };
    }

    case TOGGLE_PROCESSING: {
      return {
        // Code here
      };
    }

    case UPDATE_WIZARD_INDEX: {
      return {
        // Code here
      };
    }

    case RESET_STATE: {
      return {
        // Code here
      };
    }

    case CLEANUP_INPUT_COMPANY_INFO: {
      return {
        // Code here
      };
    }

    default: {
      return state;
    }
  }
}

以上代码均未出现错误^^^。

在关联的上下文中是这段代码,它是根据此处概述的方法构建的:https://kentcdodds.com/blog/how-to-use-react-context-effectively

import React, { createContext, useReducer, useContext } from 'react';

import { customerReducer } from './reducers/Customer';
import type { CustomerAction } from './actions/Customer';
import type { UndefinedType } from '../redux/FlowTypes';
import type { ValidationError, User } from './SharedTypes';
import type { Role } from '../utils/constants';

type Dispatch = (action: CustomerAction) => void;

type CustomerState = {
  isProcessing: boolean,
  wizardIndex: number,
  validationErrors: (?ValidationError)[],
  companyName: string
};

const defaultState: FleetCustomerState = {
  isProcessing: false,
  wizardIndex: 0,
  validationErrors: [],
  companyName: ''
};

const CustomerStateContext = createContext<CustomerState>(defaultState);
const CustomerDispatchContext = createContext<Dispatch | UndefinedType>(undefined);

const CustomerProvider = ({ children }: {children: Object}) => {
  const [state: CustomerState, dispatch: Dispatch] = useReducer(customerReducer, defaultState);
  return (
    <CustomerDispatchContext.Provider value={dispatch}>
      <CustomerStateContext.Provider value={state}>
        {children}
      </CustomerStateContext.Provider>
    </CustomerDispatchContext.Provider>
  )
}

const useCustomerState = () => {
  const context = useContext(CustomerStateContext);
  if (context === undefined) {
    throw new Error('useCustomerState must be used within a CustomerProvider');
  }

  return context;
}

const useCustomerDispatch = () => {
  const context = useContext(CustomerDispatchContext);
  if (context === undefined) {
    throw new Error('useCustomerDispatch must be used within a CustomerProvider');
  }

  return context;
}

export type {CustomerState};
export {CustomerProvider, useCustomerState, useCustomerDispatch};

然后在我的组件中,我这样调度:

dispatch({type: ADD_ERROR, payload: {name, message, id}});
dispatch({type: ADD_ERROR, payload: {name, message}});
dispatch({type: TOGGLE_PROCESSING});
dispatch({type: CLEAR_ERRORS});

这些dispatch 语句有很多错误。每个错误的措辞略有不同,但具有相同的模式。这里有两个例子:

无法使用绑定到action 的对象字面量调用dispatch,因为: 任一字符串文字 TOGGLE_PROCESSING [1] 与 属性type 中的字符串文字RESET_STATE [2]。或字符串 文字 TOGGLE_PROCESSING [1] 与字符串文字不兼容 CLEANUP_INPUT_COMPANY_INFO [3] 在属性 type 中。

无法使用绑定到action 的对象字面量调用dispatch,因为: 任一字符串文字 CLEAR_ERRORS [1] 与字符串不兼容 属性type 中的文字RESET_STATE [2]。或字符串文字 CLEAR_ERRORS [1] 与字符串字面量不兼容 CLEANUP_INPUT_COMPANY_INFO [3] 在属性type 中。

我不明白如何处理这些错误消息。任何建议将不胜感激!

【问题讨论】:

  • 你能提供dispatch的类型吗?
  • 链接到flow.org/try 会有很大帮助
  • @AlexSavin,[一如既往]感谢您的大力帮助!代码太多,将其放入 flow.org/try 将非常具有挑战性,但我已经编辑了我的帖子以显示上下文中的内容。工作理论:Flow 无法理解 reducer 中的 switch ... case 语句。你怎么看?
  • 很抱歉,如果很难重现,就很难提供帮助。我无法在沙盒中重现该问题。而且我什至无法在评论中向您发送指向工作沙箱的链接,因为 cmets 的长度有限。如果您能够在问题中提供指向沙盒的链接,那就太好了。无论如何,您需要减少问题才能看到问题的根源。

标签: javascript flowtype


【解决方案1】:

我的最佳猜测: - 将UPDATE_WIZARD_INDEX 类型的操作添加到CustomerAction - 使动作注释准确。例如,而不是

type AddErrorAction = {type: typeof ADD_ERROR, payload: ValidationError};

试试

type AddErrorAction = {|type: typeof ADD_ERROR, payload: ValidationError|}; 对于所有操作

【讨论】:

  • type 之前和ValidationError} 之后的管道字符(|)有什么作用?
  • 找到它:flow.org/en/docs/types/objects/#toc-exact-object-types 注意:你把第二个管道字符放错了地方。
  • 我有一段时间没有重新审视这个问题了。问题原来是我已经将我的一些操作移动到一个“通用”文件中 - 即。包含由多个减速器共享的那些。这会导致 Flow 出现问题,因此现在 Actions 文件和 Reducers 文件之间存在一对一的关系,并且与此相关的所有错误都消失了。
猜你喜欢
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
  • 2021-02-17
  • 1970-01-01
  • 2020-01-18
  • 1970-01-01
相关资源
最近更新 更多