• Jobs
  • About Us
  • Jobs
    • Home
    • Jobs
    • Courses and challenges
  • Businesses
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Hire tech talent
    • Blog
    • Sales
    • Salary Calculator

0

105
Views
Cuando el valor no se modifica normalmente en el estado usando redux

Cuando se hace clic en el botón Me gusta, quiero establecer Me gusta del libro en verdadero y luego ponerlo en una variable llamada LibroMe gusta.

Pero en este momento en mi código, no se agrega de inmediato.

 case ADD_TO_LIST : return { ...state, books: state.books.concat({ title: action.item.title, contents: action.item.contents, thumbnail: action.item.thumbnail, liked: false }), }; case LIKED : return { ...state, books : state.books.map((book) => book.title === action.item.title ? {...book, liked: true} : book), likedBook : state.books.filter(((book)=> book.liked === true)) } export interface Book{ title : String, contents: String, thumbnail: String, liked: boolean } const initialState : {books: Book[]; likedBook: Book[];}= { books : [], likedBook: [] };

ingrese la descripción de la imagen aquí

Me gusta es cierto, pero no se agrega a LikeBook. ¿Qué pasa con mi código? ¿Y cómo lo arreglo?

over 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Primero buscaría el libro, luego alternaría la propiedad like por title y agregaría el libro completo a la matriz de libros likedBook .

 case LIKED : // search if book already in state to like const book = state.book.find(book => book.title === action.item.title); // book found if (book) { // create new liked book object const likedBook = { ...book, liked: true }; return { ...state, // map to new books array, add new liked book or existing book books: state.books.map((book) => book.title === action.item.title ? likedBook : book ), // append to new likedBook array likedBook : state.likedBook.concat(likedBook), } } // book not found, return current state return state;
over 3 years ago · Juan Pablo Isaza Report

0

En la declaración de devolución del caso LIKED, no se ejecuta línea por línea.

Modifique el código como este.

 case LIKED : return { ...state, books : state.books.map((book) => book.title === action.item.title ? {...book, liked: true} : book), likedBook : [...state.likedBook, state.books.find(book => book.title === action.item.title)] }
over 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

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