useReducer 후크, 작업을 입력하는 방법?
Sep 20 2020
나는 여기 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
...
}
답변
2 NearHuscarl Sep 21 2020 at 04:45
당신의에서 initialState, content.file빈 문자열로 설정되어 있지만,해야 File대신
type Content = {
file: File;
...
};
type InitialState = {
content: Content;
...
};
const initialState = {
content: {
file: "",
},
...
};
해당 유형으로 null 객체를 초기화하여 수정할 수 있습니다.
const EMPTY_FILE = Object.freeze(new File([""], "filename"));
const initialState = {
content: {
file: EMPTY_FILE,
},
...
};
감속기에도 작은 오류가 있습니다.
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 } };
...
}