【发布时间】:2019-06-16 17:40:57
【问题描述】:
这快把我逼疯了!我已经尝试了所有这些技巧来使用生命周期方法来渲染这些数据,以将道具数据与我的组件中的行数据的当前状态进行比较,但看起来可能带有行道具的 React Grid 没有更新,因为我可以看到控制台在我的控制台中记录带有数据的行。它有数据,但在我的表格中显示“无数据”,这意味着当我的表格呈现时,必须有“无数据”。现在,如果我对组件进行简单的更改,点击保存,它的更新和繁荣我在表中看到 3 个条目。奇怪的是,现在我的控制台中有 6 个条目。
export default class Demo extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
columns: [
{ name: 'teamName', title: 'Team' },
{ name: 'profilePic', title: 'Avatar' },
{ name: 'score', title: 'Score' },
{ name: 'profileName', title: 'profileName' },
{ name: '', title: 'Rank' }
],
rows: [],
pageSizes: [5, 10, 15],
currentPage: 0,
loading: true,
counter: 0
};
}
componentWillMount() {
this.getRows();
}
componentDidMount() {
this.getRows();
}
getRows = () => {
this.setState({
rows: this.props.league
});
};
// static getDerivedStateFromProps(props, state) {
// if (props.league.length !== state.rows.length) {
// return {
// rows: props.league
// };
// }
// return null;
// }
// componentDidMount() {
// console.log("NP1::",this.props.league)
// this.setState({rows: this.props.league})
// }
// componentDidUpdate(nextProps, prevState) {
// if(nextProps.league!==prevState.rows){
// //Perform some operation here
// console.log("NP2::",this.props.league)
// this.setState({rows: this.props.league});
// this.classMethod();
// }
// }
// static getDerivedStateFromProps(nextProps, prevState){
// if(nextProps.league!==prevState.rows){
// console.log("NP::",nextProps.league)
// return{
// rows: nextProps.league
// }
// return
// }
// else return null;
// }
// componentDidUpdate(nextProps, prevState) {
// if(nextProps.league!==prevState.rows){
// //Perform some operation here
// console.log("NP2::",this.props.league)
// this.setState({rows: this.props.league});
// this.classMethod();
// }
// }
// componentWillReceiveProps(nextProps) {
// console.log("NP::",nextProps.league+"----"+this.props.league);
// if (nextProps.league !== this.props.league) {
// console.log("NP2::",nextProps.league)
// this.setState({rows: nextProps.league})
// }
// }
// componentWillUpdate(nextProps, nextState) {
// console.log("NP::", nextProps.league + "----" + this.state.rows);
// if (nextProps.league !== this.state.rows) {
// this.setState({ rows: nextProps.league })
// }
// }
render() {
const { rows, columns } = this.state;
console.log("STATEDATA::", this.state)
return (
<Paper>
<Grid
rows={rows}
columns={columns}
>
<RowDetailState />
<Table
cellComponent={Cell}
/>
<TableHeaderRow />
<TableRowDetail
contentComponent={RowDetail}
/>
</Grid>
{/* <PagingPanel /> */}
</Paper>
);
}
}
我在注释代码中留下了,所以你可以看到我玩过的 LF 方法。这是没有数据的第一个渲染的样子,以及包含行的状态数据的控制台日志。
我的行的初始状态是 [] 所以我们知道 setState 发生了。为什么这不会触发此组件中的渲染。我在 Ag Grid 上看到了一些关于此的帖子,但是 React 网格是否存在任何已知问题并使其正确呈现?
如前所述,在 Vs 代码中,如果我只是执行一行返回(或进行任何更改)并保存。组件更新。如此处所示,我将在控制台数组中渲染 3 个条目和 6 个条目。
添加此项以便您可以同时查看状态联赛道具。
这里是父组件
class LeaguePage extends Component {
static propTypes = {
loadLeague: PropTypes.func.isRequired,
}
constructor(props) {
super(props)
this.state = {
};
}
componentWillMount() {
this.props.loadLeague();
}
render() {
console.log("LEAGUEPROPS::",this.props);
return (
<div className="g-row">
<div className="g-col">
<h1>LeaguePage</h1>
{/* <LeagueTable {...this.props} /> */}
<br />
<br />
<RgDetail {...this.props} rows={this.state.rows} />
<br />
<br />
{/* <RgTable /> */}
<br />
<br />
<br />
</div>
</div>
)
}
}
const mapStateToProps = (state, ownProps) => {
console.log("MYSTATE::",state)
return {
league: LeagueSelector(state),
}
}
const mapDispatchToProps = Object.assign(
{},
leagueActions
);
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(LeaguePage));
联赛选择器
import { createSelector } from 'reselect';
import _ from 'lodash';
var data = [];
export function getLeague(state) {
if (state.league) {
return state.league;
}
}
function transform(UserRecord) {
const outerObj = UserRecord[Object.keys(UserRecord)[0]];
data.push(outerObj);
return data
}
export function getSortedLeague(state) {
if (state.length > 1) {
state.map(x => transform(x));
return data;
}
else
return data;
}
//=====================================
// MEMOIZED SELECTORS
//-------------------------------------
export const LeagueSelector = createSelector(
getLeague,
getSortedLeague
);
【问题讨论】:
-
你只在组件第一次挂载时调用
getRows(),所以state.rows和props.league不同步。尝试在<Grid ...>元素中直接使用this.props.league -
我试过了。当我使用
<Grid rows={this.props.league} columns={columns}>时,我得到“无数据” -
您能否在您的
render()函数中添加一个console.log(this.props.league)并检查它在渲染时是否实际包含数据? -
我看到你从道具设置状态..你为什么不在构造函数中做呢?你在那里得到道具。这是相同的道具。
-
@MTCoster 我为道具添加了更新的图片。
标签: javascript reactjs devextreme