![](/img/trans.png)
[英]Why does my React grandchild component not update after receiving a change in props from its parent? Or: do I need to use state?
[英]React Need help getting my component update after State Change
這是我第一個自己的React項目,我基本上是在嘗試添加配方。
我的結構如下所示:
我現在最大的問題是當我添加新評論時。
this.state
中的this.state更新。 但這不會將更新的數據發送到我的組件,這就是我無法確定的。
現在,我通過NavLink將數據作為狀態發送。
<NavLink to={{pathname: '/recipe/' + recipe.Name, state: { recipe: recipe, key: recipe.id }}}>{recipe.Name}</Link>
然后從Recipe.js將其作為道具發送:
<Comments comments={this.props.location.state.recipe.Comments} />
我猜這是因為我通過location
而不是props
發送食譜嗎? 但是我不知道如何更改它,以使其能夠工作並在“推薦蜜蜂”添加后重新渲染。
這是我在App.js中添加注釋時的代碼:
addComment(key, aComment)
{
aComment['UserID'] = this.state.currentUser.m;
aComment['Time'] = new Date().getTime();
const commentRef = firebase.database().ref(`/recipes/${key}/Comments`);
commentRef.push(aComment);
}
您可以從位置prop將其添加到構造函數中的組件狀態
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.