【问题标题】:React Redux: Data from src/components not updated on localhostReact Redux:来自 src/components 的数据未在 localhost 上更新
【发布时间】:2020-01-16 13:03:34
【问题描述】:

我的 src/components 和 src/reducers 中的数据存在问题。当我在那里进行更改时,我在 localhost:3000 上看不到它们。

在本地主机上我看到:

“这是书名 这是书的描述

美元。 499.99"

我的数据我看到了《指环王》

如何使这些更改在 localhost 上可见?

https://github.com/ivanradunkovic/bookstore

例如:

// BOOKS REDUCERS
export function booksReducers(state={
    books: 
        [{
            _id: 1,
            title: 'The Lord of the Rings: The Fellowship of the Ring',
            description: 'A meek Hobbit from the Shire and eight companions set out on a journey to destroy the powerful One Ring and save Middle-earth from the Dark Lord Sauron.',
            price: 20.01
        }, 
        {
            _id: 2,
            title: 'This is a second book title',
            description: 'This is the second book description',
            price: 325.25
        }]
}, action) {
    switch(action.type) {
        case "GETT_BOOK": 
        return {...state, books:[...state.books]};

        case "POST_BOOK": 
        // let books = state.books.concat(action.payload);
        // return {books};
        return {books:[...state.books, ...action.payload]};

        case "DELETE_BOOK":
        // Create a copy of the current array of books
        const currentBookToDelete = [...state.books]
        // Determine at which index in books array is the book to be deleted
        const indexToDelete = currentBookToDelete.findIndex(
          function(book){
            return book.title === action.payload;
          }
        )
        //use slice to remove the book at the specified index
        return {books: [...currentBookToDelete.slice(0, indexToDelete), ...currentBookToDelete.slice(indexToDelete + 1)]}

        case "UPDATE_BOOK":
        // Create a copy of the current array of books
        const currentBookToUpdate = [...state.books]
        // Determine at which index in books array is the book to be deleted
        const indexToUpdate = currentBookToUpdate.findIndex(
          function(book){
            return book._id === action.payload._id;
          }
        )
        // Create a new book object with the new values and with the same array index of the item we want to replace. To achieve this we will use ...spread but we could use concat methos too
        const newBookToUpdate = {
          ...currentBookToUpdate[indexToUpdate],
          title: action.payload.title
        }
        // This Log has the purpose to show you how newBookToUpdate looks like
        console.log("what is it newBookToUpdate", newBookToUpdate);
        //use slice to remove the book at the specified index, replace with the new object and concatenate witht he rest of items in the array
        return {books: [...currentBookToUpdate.slice(0, indexToUpdate), newBookToUpdate, ...currentBookToUpdate.slice(indexToUpdate + 1)]}
      }
    return state;
};



【问题讨论】:

  • 你如何在代码中使用这个 reducer?
  • 这是错字吗? switch(action.type) { case "GETT_BOOK": 那里有两个T
  • 如果您可以发布更多代码,例如您在哪里使用此减速器。此外,您应该考虑将您的状态放入一个名为 const initialState = {} 的变量中
  • 我在 github 上搞砸了一些东西。你能检查一下这个 repo 中的代码吗?谢谢你的帮助。 github.com/ivanradunkovic/bookstore

标签: reactjs redux visual-studio-code


【解决方案1】:

一种更有效的方法是使用更少的代码,将数据存储为对象图,并以图书 ID 为键。

所以你的初始状态会喜欢

books: {
  1: {
       id: 1,
       title: 'Lord of the rings',
       ...the rest
     },
  2: {
       id: 2,
       ... the rest
     }
}

在你的 reducer 函数中,你可以使用 id 键更轻松地查找或更新项目

case 'UPDATE_BOOK':
  return {
    ...state.books,
    [action.payload.id]: action.payload
  }

case 'DELETE_BOOK'
  const next = {...state.books}
  delete next[action.payload]

  return {
    ...state,
    books: next
  }

我意识到这并不能完全解决您的问题,我只是为您提供另一种存储数据的方法,这意味着您不必在任何地方都对数组进行过滤。

返回值只需Object.values(state.books)

【讨论】:

  • @sina-farhadi 我批准了您的编辑,但如果状态对象中有其他属性,它们将不会被返回。也许我原来的状态对象并没有真正表现得很好。
  • 还是什么都没有。我想编辑代码,当我进行更改时,我可以在 localhost 中看到它们。现在我一直有错误的数据。
  • 就像我说的,这不是一个解决方案,它为您提供了另一种思考数据结构的方式。
  • 是的,我知道。我可以更改数据结构,但问题仍然存在。当我更改页脚中的数据时,我仍然会看到旧数据。
  • 你能看到你的actions是否被调度并且你的reducer是否正在运行?我目前无法提取您的回购并运行它
猜你喜欢
  • 1970-01-01
  • 2020-01-16
  • 1970-01-01
  • 1970-01-01
  • 2018-01-04
  • 2018-09-24
  • 2019-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多