【问题标题】:ReactJs render doesn't update properly on data changeReactJs 渲染无法在数据更改时正确更新
【发布时间】:2017-11-16 04:45:55
【问题描述】:

编辑:下面是以两种不同的方式创建的组件,但都没有工作。仍然需要帮助。

使用 createReactClass 的解决方案 1

//Here are imports of my component


var ListPeople = createReactClass({
  people: [],

  componentWillReceiveProps: function(nextProps) {
    this.people = [];
    if(nextProps.peopleSuccess.content.length > 0) {
      this.people = [];
      for(var i = 0; i < nextProps.peopleSuccess.content.length; i++) {
        this.people.push(<Person
      checkboxValue={nextProps.peopleSuccess.content[i].checkboxValue}
      email={nextProps.peopleSuccess.content[i].email}
      emailValidation={nextProps.emailValidation}
      handleEmailChange={nextProps.handleEmailChange}
      handleOnBlur={nextProps.handleOnBlur}
      handlePersonSelect={nextProps.handlePersonSelect}
      handlePhoneChange={nextProps.handlePhoneChange}
      handleUsernameChange={nextProps.handleUsernameChange}
      id={i}
      identity={nextProps.peopleSuccess.content[i].identity}
      key={i}
      name={nextProps.peopleSuccess.content[i].name}
      phone={nextProps.peopleSuccess.content[i].phone}
      phoneValidation={nextProps.phoneValidation}
      usernameValidation={nextProps.usernameValidation} />);
  }
 }
},

  render: function() {
  console.log('this.props.peopleSuccess: ', this.props.peopleSuccess);
    return (
      <tbody>
        {this.people}
      </tbody>
    );
  }
});


export default ListPeople;

将组件作为函数的解决方案 2

import React from 'react';
//import createReactClass from 'create-react-class'
import Person from '../components/Person';

function ListPeoplePeople(props) {
  const { peopleSuccess, emailValidation, handleEmailChange, handleOnBlur, 
          handlePersonSelect, handlePhoneChange, handleUsernameChange, 
          phoneValidation, usernameValidation } = props;
  let people = [];
  for(var i = 0; i < peopleSuccess.content.length; i++) {
    people.push(<Person
      checkboxValue={peopleSuccess.content[i].checkboxValue}
      email={peopleSuccess.content[i].email}
      emailValidation={emailValidation}
      handleEmailChange={handleEmailChange}
      handleOnBlur={handleOnBlur}
      handlePersonSelect={handlePersonSelect}
      handlePhoneChange={handlePhoneChange}
      handleUsernameChange={handleUsernameChange}
      id={i}
      identity={peopleSuccess.content[i].identity}
      key={i}
      name={peopleSuccess.content[i].name}
      phone={peopleSuccess.content[i].phone}
      phoneValidation={phoneValidation}
      usernameValidation={usernameValidation} />);
  }
  return (
      <tbody>
        {people}
      </tbody>
    );
}

export default ListPeoplePeople;

当我将 console.log() 放入渲染器时,它记录了正确的数据,现在数据来自连接到 Redux 存储的父组件,最初是从 REST API 获取的。

react/redux 包版本:

"react": "^15.5.4",
"react-bootstrap": "^0.31.0",
"react-dom": "^15.5.4",
"react-redux": "^5.0.4",
"react-router": "^4.1.1",
"react-router-dom": "^4.1.1",
"react-router-redux": "^5.0.0-alpha.6",
"redux": "^3.6.0"

【问题讨论】:

  • 您从哪里获取数据?是在这个组件中还是在容器组件中?
  • 数据来自 REST API,并通过 props: dataIUse: state.dataIUseComingFromReduxStore 传递给 People 组件。
  • 好的,但是您从哪个组件中获取数据?是这个组件(人员),还是父/容器人员组件?
  • 上面的代码当然不是我原来的组件,但是与这个问题相关的所有东西都在这里。我认为当数据更改时,react 不会正确重新渲染。为什么会这样?
  • 挂载时在父组件中完成获取。并且渲染中的 console.log(this.people) 中的数据是正确的。

标签: reactjs react-redux jsx


【解决方案1】:

在我看来,这似乎是对 react-redux 连接方法的不当使用。您应该在 container 组件上调用 connect,而不是像 People 组件那样的功能性无状态组件。所以正确的用法应该是这样的:

// Container
function Container(props) {
  // get 
  return <People 
    dataIUse={props.dataIUse}
    handleCheckBoxChange={switchChecboxes}
  />
}

const mapStateToProps = (state) => {
  return {
    dataIUse: state.dataIUseComingFromReduxStore
  };
};

const mapDispatchToProps = (dispatch) => {
  return {
switchChecboxes: (data, index) => dispatch(changeChecboxValue(data, index))
  };
};

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

// People
function People(props) {
  const { dataIUse } = props;
  let people = [];
  for(var i = 0; i < dataIUse.length; i++) {
    people.push(<Person
      checkboxValue={dataIUse[i].checkboxValue}
      email={dataIUse[i].email}
      id={i}
      identity={dataIUse[i].identity}
      key={i}
      name={dataIUse[i].name}
      onChange={props.handleCheckBoxChange}
      phone={dataIUse[i].phone}/>);
  }
  return (
    <tbody>
      {people}
    </tbody>
  ); 
}

我没有使用 React.createClass,而是使用了函数。它们应该可以工作,但请确保您在文件顶部导入 react。

【讨论】:

  • 我会尝试这个建议,但在我的情况下,数据的来源不是问题,而是数据的呈现。而且我看不出这是如何解决的。
  • 我将此标记为已接受的答案,但意识到它实际上并没有工作,所以仍然存在问题。我将添加对我尝试过的两种不同解决方案的评论。
猜你喜欢
  • 1970-01-01
  • 2018-04-24
  • 1970-01-01
  • 2022-01-18
  • 2022-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多