簡體   English   中英

未在componentDidUpdate中加載的React道具

[英]React props not loaded in componentDidUpdate

在反應中,如果this.props已加載,我該怎么辦? 我嘗試了componentDidUpdate來檢查@props是否與prevProps不同,但我發現它們始終是同一件事:-/

當@ props.account.subbableProperties不為空時,我想調用以下函數。

uiActions.curliesPropertyReplacement
  element: @textarea
  words: @props.account.subbableProperties
  _this: @
  replaceProperty: true
  selectOnTop: true
  maxCount: 3
  callback: (e, value, strategy) =>
    @setState text:@textarea.text()

當我將以上函數放入componentDidMount中時,@ props.account.subbableProperties為空白(尚未加載),因此無法將其放置在那里。

我嘗試使用componentDidUpdate,但由於某種原因,此警報從未觸發!

componentDidUpdate (prevProps, prevState) ->
 if !(_.isEqual(prevProps.account.subbableProperties, @props.account.subbableProperties))
     alert("they do differ!")

我也在componentWillReceiveProps中嘗試過:

  componentWillReceiveProps: (newProps) ->
    console.log "newProps", newProps.account.subbableProperties
    console.log "newProps", newProps.account.subbableProperties.length
    console.log "props", @props.account.subbableProperties
    console.log "props", @props.account.subbableProperties.length

    if !(_.isEqual(newProps.account.subbableProperties, @props.account.subbableProperties))
      alert("it happens in willReceiveProps!")

似乎newProps和@props同時更新: 日志 我究竟做錯了什么?

更改組件后會觸發componentDidUpdate,此更改來自DOM,因此總是在組件重新呈現自己時觸發。

我不明白您在說什么,因為我不知道this.props會在哪里,但是您嘗試了componentWillReceiveProps()嗎? 這似乎更正確,因為您正在等待一些道具而不是整個組件的更新。

編輯:好的,我只留下我的信息,看到你更新了你的帖子。 因此,您確實已經嘗試了componentWillReceiveProps並提供了更多信息。 如果您正在等待更改某些道具或狀態,則應該嘗試componentWillUpdate看看它們是否有所不同。 如果您有一些新聞,我會稍后再檢查:^)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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