【发布时间】: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