Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

303
Visualizações
¿Cómo puedo definir un tipo en Initialstate en Redux-toolkit?

Quiero definir un tipo para el valor inicial en el estado de corte. Pero no sé qué hacer.

Si ingreso nulo en singlePost, recibo esta advertencia de línea roja.

pero quiero especificar el tipo del valor inicial de singlePost.

ingrese la descripción de la imagen aquí

singlePost contendrá estos valores.

ingrese la descripción de la imagen aquí

este es mi codigo

 // singlePost = { // Comments: [ // {id: 46, content: "hello", createdAt: "2022-04-30T08:24:15.000Z", // } // {id: 47, content: "zz", createdAt: "2022-04-30T08:24:30.000Z", // ], // Images: [ // PostId: 61 // createdAt: "2022-04-27T08:17:51.000Z" // id: 6 // src: "bd8b355e-cfe8-4c50-be4c-6c4958958fc6.JPEG" // updatedAt: "2022-04-27T08:17:51.000Z"] // Likecomments: [{},{}] // Likers: [{}] // User:{id:2, nickname:'elon'} // content:"helloword" // createdAt:"22022-212" // id: 61, // updatedAt:"asdasd" // } export const initialState = { singlePost: null, } const postSlice = createSlice({ name: 'post', initialState, reducers: {}, extraReducers: builder => builder .addCase(addComment.fulfilled, (state, action: PayloadAction<test>) => { const post = state.mainPosts.find(v => v.id === action.payload.PostId); state.addCommentLoading = false; state.addCommentDone = true; state.singlePost.Comments.push(action.payload); post.Comments.push(action.payload); }) .addDefaultCase(state => state), });
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

interface IImage { PostId: number; createdAt: string; id: number; //.... } interface IComment { id: number; content: string; createdAt: string; } inteface ISinglePost { Comments: IComment[] | null; Images?: IImage[]; } interface IInitialState { singlePost: ISinglePost | null; } export const initialState: IInitialState = { singlePost: null, }
about 4 years ago · Juan Pablo Isaza Relatório

0

Hay muchos ejemplos en los documentos para createSlice .

Necesitas una interfaz

 interface MySliceState { singlePost: null | { Comments: Array<{ id: number, content: string, ... }>, Images: ..., ... } }

y luego afirmar que inialState es de ese tipo.

 const initialState = { singlePost: null, } as MySliceState
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda