【发布时间】:2018-11-12 16:58:08
【问题描述】:
我正在创建一个简单的应用程序,但在重新渲染子组件时遇到了问题。在父组件(App)中调用 setState 时,传递给子组件(CardContainer)的道具不会导致它重新渲染。我知道我需要以某种方式在 App 中使用一个函数来调用 CardContainer 中的 setState,但我对如何做到这一点感到困惑。 App 中的状态肯定会发生变化(它显示在 console.logs 中),但是 我需要 CardContainer 来重新渲染它在 App 状态发生变化后映射的数组。有任何想法吗?谢谢!
这里是App代码(父组件):
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;
【问题讨论】:
标签: javascript reactjs rendering setstate