草庐IT

javascript - componentWillReceiveProps 和 componentDidUpdate 无限循环

coder 2024-05-14 原文

我试图在从子组件更改状态后更新状态但没有成功,每次我调用该函数时都会出现堆栈溢出,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,首先在父组件使用新 Prop 更新时触发,触发父组件的更新(使用 historyToggleStates) .

但是对于这个周期取决于以下几点:

if (nextprops.forceFlip && last.id === nextprops.keyId) {

即如果组件传入的 prop 的 keyId 与历史记录中的最后一个组件的 keyId 相同。换句话说,当子级更新时,如果 forceFlip 为真,则历史记录中的最后一个组件将始终运行此代码块。

forceFlip 的值取决于以下内容:

    if (history.length > 1) {
       if (JSON.stringify(last.opened) === JSON.stringify(beforeLast.opened)) {

forceFlip 如果历史记录中的最后两个组件同时打开,则设置为 true。

然后,正如您所说,historyToggleStates 被触发,父级得到更新,子级的 componentWillReceiveProps 被触发,然后循环重复。

可能的解决方法

现在我认为它是有意设计的,如果最后两张牌同时打开,它们必须被强制翻转,即关闭。

为了实现这一点,我建议不要将卡片的状态保留在卡片组件中,而是将其拉至父组件状态并让子卡片组件不可知。在父级中,维护类似 cardsState(也许是更好的名字)的东西,并用它来决定是否必须打开卡片。如果您检测到最近两个同时打开的卡片在历史记录中处于打开状态,只需将它们的状态设置为 false 即可关闭它们。

这将使您摆脱对 forceFlip 变量的依赖,并使您的子卡片组件更简单 - 只有在状态显示打开时才打开它们。

关于javascript - componentWillReceiveProps 和 componentDidUpdate 无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45270253/

有关javascript - componentWillReceiveProps 和 componentDidUpdate 无限循环的更多相关文章

随机推荐