我试图从子组件更改状态后尝试更新状态,但没有成功,每次我调用函数时都会出现堆栈溢出, Prop 正在无限次调用该函数,但是问题是,我确实需要更新这种状态,目前不知道如何解决。
父
import React, { PropTypes, Component } from 'react';
import Card from './card/card.js';
import style from './style.scss';
class Container extends Component {
constructor(props) {
super(props);
this.state = {
isFlipped: false,
oneOpened: true,
history: [],
childFlipToFalse: false,
};
this.historyToggleStates = this.historyToggleStates.bind(this);
this.forceFlipParent = this.forceFlipParent.bind(this);
this.checkForceFlip = false;
}
historyToggleStates(bool, id, callForceFlip) {
this.setState({
history: this.state.history.concat([{ opened: bool, id }]),
}, () => {
console.log('inside historyToggleStates');
if (callForceFlip) {
this.forceFlipParent()
}
});
}
forceFlipParent() {
const { history } = this.state;
const first = history[0];
const last = history[history.length - 1];
const beforeLast = history[history.length - 2];
console.log('force FLIP PARENT');
if (history.length > 1) {
if (JSON.stringify(last.opened) === JSON.stringify(beforeLast.opened)) {
this.setState({ childFlipToFalse: true });
}
}
}
render() {
const rest = {
basePath: this.props.basePath,
backCard: this.props.backCard,
isShowing: this.props.isShowing,
historyToggleStates: this.historyToggleStates,
isOpened: this.state.isOpened,
isFlipped: this.state.isFlipped,
checkOneOpened: this.checkOneOpened,
history: this.state.history,
forceFlip: this.state.childFlipToFalse,
flipToFalse: this.forceFlipParent,
};
const cardsMap = this.props.cards.map((item, key) => {
return (
<Card
item={item}
keyId={key}
{...rest}
/>
);
});
return (
<div className="col-lg-12 text-center">
{cardsMap}
</div>
);
}
}
export default Container;
Container.propTypes = {
cards: PropTypes.array.isRequired,
item: PropTypes.func,
basePath: PropTypes.string,
backCard: PropTypes.string,
isShowing: PropTypes.bool,
};
child
import React, { Component, PropTypes } from 'react';
import ReactCardFlip from 'react-card-flip';
import style from './style.scss';
class Card extends Component {
constructor(props) {
super(props);
this.state = {
isFlipped: false,
update: false,
id: 9999999,
};
this.handleClick = this.handleClick.bind(this);
this.checkOneOpened = this.checkOneOpened.bind(this);
}
componentWillReceiveProps(nextprops) {
const { history, isFlipped, historyToggleStates } = this.props;
const last = nextprops.history[nextprops.history.length - 1];
const beforeLast = nextprops.history[nextprops.history.length - 2];
console.log(history);
console.log(nextprops.history);
if (nextprops.forceFlip && last.id === nextprops.keyId) {
this.setState({ isFlipped: !this.state.isFlipped, update: true, id: last.id }, () => {
console.log('callback willreceiveprops', this.state.isFlipped);
historyToggleStates(this.state.isFlipped, nextprops.keyId, true, this.state.update); **<--- Here's my problem**
});
}
if (nextprops.forceFlip && beforeLast.id === nextprops.keyId) {
this.setState({ isFlipped: !this.state.isFlipped, update: true, id: beforeLast.id }, () => {
});
}
}
handleClick(e, nextState, id) {
const { keyId, historyToggleStates, forceFlip } = this.props;
if (e) {
e.preventDefault();
}
if (!nextState) {
this.setState({ isFlipped: !this.state.isFlipped }, () => {
historyToggleStates(this.state.isFlipped, keyId, true, this.state.update);
});
} else {
// historyToggleStates(nextState, id, false);
return 0;
}
}
checkOneOpened(e) {
if (!this.props.isShowing) {
this.handleClick(e);
}
}
render() {
const { item, basePath, backCard, isShowing, isFlipped, forceFlip } = this.props;
return (
<div className={`col-lg-2 col-md-3 col-sm-6 ${style.card}`}>
<ReactCardFlip
isFlipped={this.state.isFlipped}
flipSpeedBackToFront={0.9}
flipSpeedFrontToBack={0.9}
>
<div key="front">
<button
onClick={() => {this.checkOneOpened()}}
>
<img src={isShowing ? `${basePath}${item.image}` : backCard} alt={item.name} className={`${style.img}`} />
</button>
</div>
<div key="back">
<button
onClick={() => {this.checkOneOpened()}}
>
<img src={isShowing ? backCard : `${basePath}${item.image}`} alt={item.name} className={`${style.img}`} />
</button>
</div>
</ReactCardFlip>
</div>
);
}
}
export default Card;
Card.propTypes = {
basePath: PropTypes.string,
backCard: PropTypes.string,
isShowing: PropTypes.bool,
historyToggleStates: PropTypes.func,
isOpened: PropTypes.bool,
isFlipped: PropTypes.bool,
checkOneOpened: PropTypes.func,
};
historyToggleStates(this.state.isFlipped,nextprops.keyId,true,this.state.update)是我问题的根源,我真的需要更新它,因为我正在将他内部的数组与另一个数组进行比较
更新1:我知道我在两种情况下都完成了对historyToggleStates的调用,但是如您所见,我需要从父级更新状态,因为我每次在子组件的componentWillReceiprops中都比较该值。
在这种情况下,真的需要州经理吗?我遵循Dan Abramov的技巧,并避免提高系统的复杂性,任何技巧都将不胜感激。
最佳答案
子组件的componentWillReceiveProps
,首先由父组件使用新的props更新时触发,然后触发父组件的更新(带有historyToggleStates
)。
但是,此循环取决于以下条件:
if (nextprops.forceFlip && last.id === nextprops.keyId) {
即组件的传入 Prop 的
keyId
是否与历史记录的keyId
中的最后一个组件相同。换句话说,当子代得到更新时,只要forceFlip
为true,历史记录中的最后一个组件将始终运行此代码块。forceFlip
的值取决于以下内容: if (history.length > 1) {
if (JSON.stringify(last.opened) === JSON.stringify(beforeLast.opened)) {
也就是说,如果历史记录中的后两个组件同时打开,则
forceFlip
设置为true。然后,正如您所说的
historyToggleStates
被触发, parent 被更新, child 的componentWillReceiveProps
被触发并且循环重复。可能的解决方法
现在,我认为这是故意设计的,如果最后两张卡同时打开,则必须强制翻转即关闭它们。
为此,我要说的是不要将卡的状态保持在卡组件中,而是将其拉到父组件的状态,并使子卡组件不可知。在父级中,维护类似于
cardsState
(也许是一个更好的名称)的名称,并使用它来决定是否必须打开卡。如果在历史记录中检测到最后两个同时打开的卡,只需将它们的状态设置为false,就可以关闭它们,从而关闭它们。这将使您摆脱对
forceFlip
变量的依赖,并使子卡组件更简单-仅在状态为open时打开它们。