Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
cómo actualizar un objeto dentro de una matriz durante una acción asyncThunk.fulfilled en Redux

Tengo un método AsyncThunk llamado likePost cuando un usuario hace clic en Me like en una publicación, enviará esta acción a través del envío. El método funciona bien, y en mi base de datos, la Post se actualiza con éxito, pero no puedo hacer que Redux actualice la publicación que me gusta durante el método .fulfilled .

Esto es con lo que estoy trabajando actualmente:

 // shoes = [{_id: '', title: '', likes: []}, ...{}] export const likePost = createAsyncThunk( 'posts/likePost', async (payload, { rejectWithValue }) => { try { const response = await userTokenAxios.post(`/${payload}/like`); return response.data; } catch (error) { return rejectWithValue(error.response.data); } } ); [likePost.fulfilled]: (state, action) => { const post = state.posts.find((post) => post._id === action.payload._id); post.likes.push(action.payload.user); },
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

En lugar de encontrar la publicación, obtenga el índice de ese objeto en state.posts const post = state.posts.find((post) => post._id === action.payload._id);

 const postIndex=state.posts.findIndex((post)=> post._id === action.payload._id);

Ahora puede empujar al user en la matriz de Me gusta:

 state.posts[postIndex].likes.push(action.payload.user)
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!