【发布时间】: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