使用父组件在React中重新呈现子组件

时间:2018-06-03 00:15:49

标签: javascript reactjs rendering setstate

我正在创建一个简单的应用程序,但我无法重新渲染子组件。在父组件(App)中调用setState时,传递给子组件(CardContainer)的props不会导致它重新呈现。我知道我需要以某种方式使用App中的函数来调用CardContainer中的setState,但我很难过如何做到这一点。应用程序中的状态肯定会更改(它显示在console.logs中),但我需要CardContainer重新呈现它在应用程序状态更改后映射的数组。有任何想法吗?谢谢!

以下是应用代码(父组件):

import React, { Component } from 'react';
import Header from './Header';
import AddButton from './AddButton';
import CardContainer from './CardContainer';
import style from '../style/App.css';



class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      cards: [{id: 1, title: 'first', text: 'Random stuff that is on the paage.'},
        {id: 2, title: 'second', text: 'More text goes here'},
        {id: 3, title: 'third', text: 'to doooo'}],
      cardSelected: false,
      selectedCard: []
    };
    this.addCard = this.addCard.bind(this);
    this.deleteCard = this.deleteCard.bind(this);
    this.openSelectedCard = this.openSelectedCard.bind(this);
    this.closeSelectedCard = this.closeSelectedCard.bind(this);
    this.editExistingCard = this.editExistingCard.bind(this);
  }

  openSelectedCard(card) {
    this.setState({
      cardSelected: true,
      selectedCard: card
    })
  }

  closeSelectedCard() {
    this.setState({
      cardSelected: false
    })
  }

  addCard() {
    let newId = this.state.cards.length + 2;
    this.openSelectedCard({id: newId, title: 'Untitled', text: 'Just start typing here.'});
    // this.state.cards.push('New Post It');
    // this.setState({
    //   cards: this.state.cards
    // })
  }

  editExistingCard(cardToEdit) {
    for (let i = 0; i < this.state.cards.length; i++) {
      if(this.state.cards[i].id === cardToEdit.id) {
        this.state.cards.splice(i, 1);
        this.state.cards.splice(i, 0, cardToEdit);
      }
    }
    this.setState({
      cards: this.state.cards
    }, () => {this.closeSelectedCard()})
  }

  deleteCard(index) {
    console.log(this.state.cards[index])
    this.state.cards.splice(index, 1);
    this.setState({
      cards: this.state.cards
    })
  }

  render() {
    return (
        <div className={style.appContainer}>
          <Header addCard={this.addCard}/>
          <CardContainer openSelectedCard={this.openSelectedCard} closeSelectedCard={this.closeSelectedCard} cards={this.state.cards} deleteCard={this.deleteCard} selectedCard={this.state.selectedCard} cardSelected={this.state.cardSelected} editExistingCard={this.editExistingCard}/>
        </div>
    );
  }
}

export default App;

这是CardContainer代码(子组件):

import React, { Component } from 'react';
import Card from './Card';
import SelectedCard from './SelectedCard';
import style from '../style/CardContainer.css';



class CardContainer extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selecedCard: [],
      cards: []
    };
  }

  componentWillMount() {
    this.setState({
      cards: this.props.cards
    })
  }

  render() {
    return (
        <div className={style.cardContainer}>
          {this.state.cards.map((card, index) => (
            <Card card={card} key={card.id} index={index} title={card.title} text={card.text} openSelectedCard={() => this.props.openSelectedCard(card)} deleteCard={this.props.deleteCard}/>
          ))}
          {this.props.cardSelected &&
            <div className={style.selectedCardComponentContainer}>
              <SelectedCard card={this.props.selectedCard} closeSelectedCard={this.props.closeSelectedCard} editExistingCard={this.props.editExistingCard}/>
            </div>
          }
        </div>
    );
  }
}

export default CardContainer;

5 个答案:

答案 0 :(得分:0)

不要在子组件中使用州的cards。用户this.props.cards相反,只要道具发生变化,whcih就会重新渲染你的组件。如果仍然不起作用,请使用component.forceUpdate()

&#13;
&#13;
class CardContainer extends Component {
  render() {
    return (
        <div className={style.cardContainer}>
          {this.props.cards.map((card, index) => (
            <Card card={card} key={card.id} index={index} title={card.title} text={card.text} openSelectedCard={() => this.props.openSelectedCard(card)} deleteCard={this.props.deleteCard}/>
          ))}
          {this.props.cardSelected &&
            <div className={style.selectedCardComponentContainer}>
              <SelectedCard card={this.props.selectedCard} closeSelectedCard={this.props.closeSelectedCard} editExistingCard={this.props.editExistingCard}/>
            </div>
          }
        </div>
    );
  }
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您正在调用componentWillMount中的setState,这样组件不会重新渲染,因为您正在调用尚未渲染的组件内的方法。您应该始终在React Docs上的componentDidMount上设置状态...

希望有帮助:)

答案 2 :(得分:0)

这是CardContainer生命周期的问题。 处于组件状态的cards变量仅在首次装入组件后才设置。每当组件道具更新时,您也需要设置它。

修复: 您需要在CardContainer组件中添加类似的内容:

componentWillReceiveProps(nextProps) {
  this.setState({
    cards: nextProps.cards
  });
}

编辑: 另一个解决方法是使用组件CardContainer道具中已有的卡片。

答案 3 :(得分:0)

子进程没有重新渲染,因为componentWillMount(已弃用以便停止使用它)和componentDidMount仅在初始启动时触发。此外,我发现您已经在CardContainer组件中声明了一个空数组,即使您将引入道具作为构造函数的参数。此外,SelectedCard组件正在接收props.selectedCard。看起来您的卡阵列状态根本没有使用过。

我希望你不要介意我没有给你回答,但我认为你应该稍微看一下这些要点并重新评估你的代码结构。

答案 4 :(得分:0)

感谢您的帮助!我发现我实际上需要在CardContainer的子组件Card组件中调用setState。该卡是我想要重新渲染的实际组件,所以我应该考虑确保它也获得了setState。我认为更新填充卡片的阵列就足够了。