javascript - react 终极版 : Connected child component not receiving props

标签 javascript reactjs redux react-redux

我似乎无法弄清楚问题是什么。我有一个名为 DisplayItems 的组件,它从 API(项目列表)获取一些数据。

我正在使用 Redux 操作和 thunk 中间件从 API 获取项目和数据。我将项目和其他数据作为对象数组保存到 redux 存储中,并将其命名为 entriesData。实际数组在 entriesData.data 中。我正在使用 connect 将状态和操作连接到 DisplayItems 的 Prop 。

接下来,我有一个名为 GalleryItemContentV1 的子组件。 GalleryItemContentV1 未连接到 redux。它只是从 DisplayItems 接收数据,后者循环遍历项目数组并将每个项目传递给 GalleryItemContentV1。因此,DisplayItems 循环遍历 this.props.entriesData.data 并将每个项目作为名为 entry< 的 Prop 传递给 GalleryItemContentV1/.

最后,我在 GalleryItemContentV1 中有一个名为 TestCom 的子组件。这就是问题所在。我还将我从 DisplyItem 获得的项目传递给 TestCom。因此,数据流为 DisplayItems(已连接)> GalleryItemContentV1(未连接)> TestCom(已连接)。

TestCom 连接到 redux。它使用 redux 的一些操作来更新 entriesData.data 中的项目。当我在 TestCom 中更新 this.props.entriesData.data 时,GalleryItemContentV1 收到更新后的数据就好了,但是 TestCom 没有收到任何东西。 TestCom 中的项目永远不会更新。

我发现问题出在我将 TestCom 连接到 redux 时。如果 TestCom 没有连接到 redux,它也会收到更新的 Prop ,就像 GalleryItemContentV1 一样。

组件如下。我已尝试尽可能地简化它们。

显示项

import React, {Component} from 'react';
import GalleryItemContentV1 from './GalleryItemContentV1';
import { connect } from 'react-redux';
import {getContestEntriesPageData} from '../../actions/contest';

class DisplayItems extends Component {
  componentDidMount(){
    this.props.getContestEntriesPageData(uri, search);
  }

  render(){

    console.log('DisplayItems props', this.props);

      return (
        <div>
          <div className="card-deck thumbnails" id="card-list">
            {  this.props.entriesData.data instanceof Array ? 
                  this.props.entriesData.data.map(function(object, i){
                    return <GalleryItemContentV1 
                      entry={object} key={i} 
                      arr_key={i}
                    />;
                  }, this) : ''
              }
          </div>
        </div>
      )
  }
}

const mapStateToProps = state => (
  {
    entriesData: state.entriesData,
  }
)

const mapDispatchToProps = (dispatch) => {
  return {
    getContestEntriesPageData: (uri, search) => {
      dispatch(getContestEntriesPageData(uri, search));
    }
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(DisplayItems);

GalleryItemContentV1

import React, { Component } from 'react';
import TestCom from './TestCom';

class GalleryItemContentV1 extends Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
        <div>
        <TestCom entry={this.props.entry} />
      </div>
    );
    }
}
export default GalleryItemContentV1;

测试通讯

我正在 TestCom 中输出 this.props.entry.VotesCount(来自 DisplayItems 的项目)的结果。这就是问题发生的地方。出于某种原因,当我将 TestCom 连接到 redux 时,它的 props 没有更新,但是当它与 Redux 断开连接时,它的 props 就会更新。

import React, {Component} from 'react';
import { connect } from 'react-redux';

class TestCom extends Component {
  constructor(props) { 
    super(props);
  }

  render(){

    console.log('TestCom props', this.props);

    return (

      <div>{this.props.entry.VotesCount}</div>

    )
  }
}

const mapStateToProps = state => (
  {
    user: state.user
  }
)
export default connect(mapStateToProps)(TestCom);

这里是我配置 redux 的地方,但我认为了解它并不重要,因为 GalleryItemContentV1 可以很好地看到更新后的数据。

import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import ContestReducer from '../reducers/contest';

export default function configureStore(initialState) {
  return createStore(
    ContestReducer,
    initialState,
    applyMiddleware(thunk)
  );
}

所以,总结一下。

DisplayItems(已连接,可以从商店接收更新数据)> GalleryItemContentV1(未连接,可以接收更新)> TestCom (已连接,不接收更新,但在断开连接时接收更新)

这是我的 reducer ,以防万一。

import * as ContestActionTypes from '../actiontypes/contest';

const initialState = {
  contest: null,
  subscriptions: [],
  user: null,
  page: null
}
export default function Contest(state=initialState, action) {
  console.log('redux action reducer: ', action)
  switch(action.type) {
    case ContestActionTypes.HANDLE_VOTE:
      const newState = { ...state };
      if (newState.entriesData) {
        const index = newState.entriesData.data.findIndex(x => x.id === action.entry.id)
        newState.entriesData.data[index].VotesCount = action.vote;
      } else {
        newState.entry.VotesCount = action.vote;
      }

      return newState

      default:
        return state;
  }
}

我还注意到 GalleryItemContentV1 也发生了同样的事情。如果我连接它,它就会停止从 DisplayItems/redux store 接收更新。

欢迎任何意见。谢谢!

最佳答案

你正在改变你的状态。您正在该切片缩减器中制作顶层状态的浅拷贝,但您并未制作内部所有嵌套级别的副本。然后,您连接的组件正在提取 state.entriesData,它没有通过引用更改。因此,connect 包装器组件认为状态根本没有改变,并且不会重新呈现您自己的组件。

Redux docs page on "Immutable Update Patterns"特别指出这是一个常见错误。

如果不可变地更新嵌套状态太痛苦,我建议查看 immer immutable update library .此外,我们的新 redux-starter-kit package在内部使用 Immer 让您编写更简单的 reducer。

另请参阅我的帖子 Idiomatic Redux: The History and Implementation of React-Redux有关 connect 内部如何工作的背景和详细信息。

关于javascript - react 终极版 : Connected child component not receiving props,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53767242/

相关文章:

javascript - 将焦点从子窗口返回到父窗口

reactjs - 如何触发事件 React 测试库

javascript - 尝试向操作提交 formik 表单

reactjs - Nextjs中下一页之前的页面转换

angular - Redux 状态和 Angular 2 路由

javascript - 编辑大量内容的大型(大概)应用程序

javascript - Angular 上的分离函数

javascript - 重置样式为 <li> 的选择菜单

javascript - Firebase 查询示例返回无用的对象

javascript - Reactjs:切换不同组件的类