简体   繁体   English

React 不会在状态更改时呈现更新的 UI

[英]React not rendering updated UI on state change

I'm working on a mini project where a user shall be able to edit book info.我正在做一个小项目,用户可以在其中编辑图书信息。 The app presents the user with a filter to choose Fiction or Science subject category.该应用程序为用户提供了一个过滤器来选择小说或科学主题类别。 Once chosen, he is shown a list of books where he can change the details of the book by clicking the edit button.选择后,他会看到一个书籍列表,他可以通过单击编辑按钮更改书籍的详细信息。 On editing, he can choose to either save or cancel the form details.在编辑时,他可以选择保存或取消表单详细信息。 This entire workflow is on a single page!整个工作流程都在一个页面上! I'm using React, Redux, and Redux-form for this purpose where component I - Selection category (Fiction, Science), component II - list of books for that category and component III - form to edit book details.为此,我使用 React、Redux 和 Redux-form,其中组件 I - 选择类别(小说、科学)、组件 II - 该类别的书籍列表和组件 III - 用于编辑书籍详细信息的表单。 See the attached image to imagine the app.请参阅附加图像以想象该应用程序。 在此处输入图片说明

Working of the app : Once the user chooses Fiction/Science, an action is dispatched to list available books.应用程序的工作:一旦用户选择了小说/科学,就会调度一个动作来列出可用的书籍。 Once he clicks Edit button for a book, another action is dispatched to display the form pre-filled with the book's values (so that the user can edit it).一旦他单击一本书的“编辑”按钮,就会调度另一个操作来显示预先填充了该书的值的表单(以便用户可以对其进行编辑)。

The Problem : Unfortunately, my Edit form component is not rendering desired values due to unavailability of props to this component;问题:不幸的是,由于该组件的 props 不可用,我的 Edit 表单组件没有呈现所需的值; even though I can see the success of my redux action with correct state/props.即使我可以通过正确的状态/道具看到我的 redux 操作的成功。

Anatomy of code :代码剖析

  1. SubjectChoice.jsx主题选择.jsx

const mapDispatchToProps = dispatch => ({ fetchBooks: favoriteSubject => dispatch(getBooksList(favoriteSubject)), fetchSubjects: _ => dispatch(getSubjectsList()) });

So, this component correctly dispatches getSubjectsList and I can use fetchSubjects as props on componentDidMount .所以,这个组件正确地调度getSubjectsList并且我可以使用fetchSubjects作为componentDidMount道具。 Similarly, it dispatches another action getBooksList(favoriteSubject) according to the chosen subject and fetchBooks can be used as props on handleChange callback (These actions make API request; in fact, all does)类似地,它根据选择的主题调度另一个动作getBooksList(favoriteSubject)并且fetchBooks可以用作handleChange回调的道具(这些动作发出 API 请求;实际上,所有这些动作)

  1. BookList.jsx书单.jsx

const mapDispatchToProps = dispatch => ({ fetchBookDetails: bookId => dispatch(getBookDetails(bookId)) });

So, this component correctly dispatches getBookDetails action on handleEdit which is Edit button callback.因此,该组件在handleEdit上正确调度getBookDetails操作,即编辑按钮回调。

  1. BookEdit.jsx BookEdit.jsx

const mapStateToProps = state => ({ book: state.book.bookDetails, ... }... render() { const { bookDetails, loading, error } = this.props.book; ... }

Now, this component keeps on waiting to re-render but it never receives the props.现在,这个组件一直在等待重新渲染,但它永远不会收到道具。 I see the actions, reducers dispatching correctly in the console (from BooksList.jsx component) but never see them reflecting on screen.我看到操作,reducers 在控制台中正确调度(来自BooksList.jsx组件),但从未看到它们反映在屏幕上。

How SO answers are not helping me get out of this situation : I saw answers where it's specified that redux does a shallow comparison and if it doesn't find a subtle difference in the previous and next state then it skips rendering the app. SO 答案如何没有帮助我摆脱这种情况:我看到了一些答案,其中指定 redux 进行了浅层比较,如果在前一个和下一个状态中没有发现细微差别,则它会跳过渲染应用程序。 However, as mentioned in the third step I see the action and reducer with previous and next state correctly.但是,如第三步所述,我正确地看到了具有上一个和下一个状态的动作和减速器。

If BookEdit isn't connected to the store or the props aren't being passed from a parent component you will not have access to them.如果 BookEdit 未连接到商店或道具未从父组件传递,您将无法访问它们。 Either connect your BookEdit...连接您的 BookEdit...

import { connect } from 'react-redux;
...
const mapStateToProps = ({ book }) => {
  const { bookDetails, loading, error } = book;
  return { bookDetails, loading, error };
}
export default connect(mapStateToProps)(BookEdit);

Or pass them in from a parent connected component that is bringing in props from your store.或者从从您的商店引入道具的父级连接组件传递它们。

Every time Redux actions are dispatched, mapStateToProps will be executed.每次分派 Redux 操作时,都会执行mapStateToProps The connect function from react-redux will only re-render your component when the props from mapStateToProps are not equal to the previous props. react-redux 的 connect 函数只会在mapStateToProps中的 props 不等于之前的 props 时重新渲染您的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM