簡體   English   中英

在狀態更改后,React Need需要幫助來更新我的組件

[英]React Need help getting my component update after State Change

這是我第一個自己的React項目,我基本上是在嘗試添加配方。

我的結構如下所示:

  • App.js
  • AllRecipes.js
  • Recipe.js
  • Comments.js
  • AddComment.js

我現在最大的問題是當我添加新評論時。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM