reactjs - useReducer 钩子,如何对动作进行类型检查?
问题描述
我一直在stackoverflow上查看各种资源,但还没有找到任何可以解决我遇到的问题的东西。我对打字稿有基本的了解,所以错误限制了我。
如果我留action: any
在减速器中,错误就会消失。但是,如果我输入它,Action
我会收到一个过载错误。我对 TS 还不够聪明,还没有弄清楚如何推理或解决它,它正在发生ReducerWithoutAction<any>
,但我不知道我应该如何解决这个问题。任何帮助,将不胜感激。
在此期间,我将继续四处寻找。
// state types
type MainWrapperType = {
selection: number;
};
type Word = {
word: string;
translation: string;
wordTranscription: string;
partOfSpeech: string;
description: string;
example?: string;
};
type Notification = {
format: string;
message: string;
};
type Content = {
lessonName: string;
language: string;
topics: string[];
format: string;
file: File;
};
type InitialState = {
activeStep: number;
notification: Notification;
content: Content;
newWords: Word[];
initialTranscript: string;
modifiedTranscript: string;
transcriptSentences: string[];
};
const initialState = {
activeStep: 0,
notification: {
format: "",
message: "",
},
content: {
lessonName: "",
language: "",
topics: [],
format: "",
file: "",
},
newWords: [],
initialTranscript: "",
modifiedTranscript: "",
transcriptSentences: [],
};
// action types
interface SET_FILE {
type: "SET_FILE";
payload: File;
}
interface RESET_FILE {
type: "RESET_FILE";
}
interface SET_INIT_TRANSCRIPT {
type: "SET_INIT_TRANSCRIPT";
payload: string;
}
interface SET_MODIFIED_TRANSCRIPT {
type: "SET_MODIFIED_TRANSCRIPT";
payload: string;
}
interface ADD_SENTENCE {
type: "ADD_SENTENCE";
payload: string;
}
interface SET_SENTENCE_TRANSLATIONS {
type: "SET_SENTENCE_TRANSLATIONS";
payload: string[];
}
interface ADD_NEW_WORD {
type: "ADD_NEW_WORD";
payload: Word;
}
interface UPDATE_WORD {
type: "UPDATE_WORD";
payload: Word;
}
interface INCREMENT_ACTIVE_STEP {
type: "INCREMENT_ACTIVE_STEP";
}
interface DECREMENT_ACTIVE_STEP {
type: "DECREMENT_ACTIVE_STEP";
}
interface RESET_ACTIVE_STEP {
type: "RESET_ACTIVE_STEP";
}
interface RENDER_NOTIFICATION {
type: "RENDER_NOTIFICATION";
payload: Notification;
}
type Action =
| SET_FILE
| RESET_FILE
| SET_INIT_TRANSCRIPT
| SET_MODIFIED_TRANSCRIPT
| ADD_SENTENCE
| SET_SENTENCE_TRANSLATIONS
| ADD_NEW_WORD
| UPDATE_WORD
| INCREMENT_ACTIVE_STEP
| DECREMENT_ACTIVE_STEP
| RESET_ACTIVE_STEP
| RENDER_NOTIFICATION;
const reducer = (state: InitialState, action: Action) => {
switch (action.type) {
case "SET_FILE":
return { ...state, content: action.payload };
case "RESET_FILE":
return { ...state, content: initialState.content };
case "SET_INIT_TRANSCRIPT":
return { ...state, initialTranscript: action.payload };
case "SET_MODIFIED_TRANSCRIPT":
return { ...state, modifiedTranscript: action.payload };
case "ADD_SENTENCE":
return {
...state,
transcriptSentences: [...state.transcriptSentences, action.payload],
};
case "SET_SENTENCE_TRANSLATIONS":
return {
...state,
transcriptSentences: action.payload,
};
case "ADD_NEW_WORD":
return {
...state,
newWords: [...state.newWords, action.payload],
};
case "UPDATE_WORD":
const newWordsClone = [...state.newWords];
// console.log("nwc", newWordsClone);
const seekWord = (wordObject: any) =>
wordObject.word === action.payload.word;
const wordIndex = newWordsClone.findIndex(seekWord);
// console.log("wi", wordIndex, newWordsClone[wordIndex]);
newWordsClone[wordIndex] = action.payload;
return {
...state,
newWords: newWordsClone,
};
case "INCREMENT_ACTIVE_STEP": {
return { ...state, activeStep: state.activeStep + 1 };
}
case "DECREMENT_ACTIVE_STEP": {
return { ...state, activeStep: state.activeStep - 1 };
}
case "RESET_ACTIVE_STEP": {
return { ...state, activeStep: 0 };
}
case "RENDER_NOTIFICATION":
return { ...state, notification: action.payload };
default:
throw new Error();
}
};
const UploadPage = () => {
const [state, dispatch] = useReducer(reducer, initialState); <-- Error
...
}
解决方案
在您的initialState
,content.file
中设置为空字符串,但应该File
改为
type Content = {
file: File;
...
};
type InitialState = {
content: Content;
...
};
const initialState = {
content: {
file: "",
},
...
};
您可以通过使用相应类型初始化空对象来修复它
const EMPTY_FILE = Object.freeze(new File([""], "filename"));
const initialState = {
content: {
file: EMPTY_FILE,
},
...
};
你的reducer也有一个小错误
switch (action.type) {
case "SET_FILE":
return { ...state, content: action.payload };
...
}
应该是这个
switch (action.type) {
case "SET_FILE":
return { ...state, content: { ...state.content, file: action.payload } };
...
}
推荐阅读
- youtube-analytics-api - 可通过 API 获得的 Youtube 唯一浏览量数据?
- c# - 使用文本框过滤记录时维护复选框的状态
- python - 如何在python中使用LSTM将预测值的输出反馈回输入
- c# - 如何在 Outlook 中知道收到的电子邮件是回复还是不回复?
- python - 如何在以json为值的熊猫中分组?
- mocking - 模拟设置不返回预期值
- python - 有没有更好的方法来使用烧瓶的滚动条错误报告?
- javascript - JQuery ':contains' 将其转换为 Javascript
- apache-spark - 如何在 scala/pyspark 中对 xgboost 使用单调约束
- ios - iOS 作为 BLE 外设和中央模式问题