我正在使用 firebase / redux 工具包 / typescript 創建我的第一個 react-native 應用程式,但我遇到了一些問題。
Atm 我有這種型別
type Program = {
id?: string
name: string!
trainings: Record<string, Training>
userId: string
}
在我的 redux 狀態下,我有這個
export interface ProgramState {
programs: Program[]
currentEditingProgram: Program | null
loading: boolean
}
const initialState: ProgramState = {
programs: [],
currentEditingProgram: null,
loading: true,
}
我的問題是,當我使用此功能進行更新時...
export const updateProgramById = createAsyncThunk(
"programs/updateById",
async (program: Program, thunkAPI) => {
await updateDoc(doc(db, programsCol.id, program.id!), program)
}
...我需要從 {program} 中洗掉 id 屬性以將其保存在 firestore 上:
{id}:{
name:"",
trainings:[]
userId:""
AND NOT
{id}:{
id:"",//Same as {id}
name:"",
trainings:[]
userId:""
我的問題是:
- 我應該有單獨的型別,一種有 id 一種沒有嗎?
- 當我想更新一個程式時,我應該保存我的 currentEditingProgram 物件還是只保存 id(我有一個螢屏 ProgramDetail,我可以導航到 Training 螢屏詳細資訊)
uj5u.com熱心網友回復:
你可以有一個單獨的型別,只保存資料,沒有 ID,以及一個結合了這樣的泛型型別:
打字稿劇本
//generic redux type with ID
type ReduxIdItem<T, IdType = string> = { id: IdType, data: T }
type ProgramData = {
name: string
trainings: Record<string, Training>
userId: string
}
type ProgramRedux = ReduxIdItem<ProgramData>;
const test: ProgramRedux = {} as ProgramRedux;
//now you have the object separated from the id
test.data //hods the data without id
test.id // holds the id
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/517331.html
標籤:Google Cloud Collective 打字稿火力基地反应式粗鲁redux 工具包
上一篇:如何折射柏樹的承諾代碼
下一篇:如何在物件文字中鍵入嵌套物件
