简体   繁体   中英

React props not loaded in componentDidUpdate

In react, how do I do something if this.props have been loaded?? I tried componentDidUpdate to check to see if @props are different to prevProps but I found that they are always the same thing :-/

I want to call the function below when @props.account.subbableProperties is not blank.

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

When I put the above function in componentDidMount, @props.account.subbableProperties is blank (hasn't yet loaded) so I can't put it there.

I tried using componentDidUpdate but for some reason this alert never triggers!

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

I also tried it in 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!")

It seems like newProps and @props update at the same time: 日志 What am I doing wrong?

componentDidUpdate is triggered after you've a change in your component, this change comes from the DOM so will be triggered always when the component re-render himself.

I didn't understand much of what you saying because I don't know where this.props is coming but, did you tried componentWillReceiveProps() ? This seems more properly because you're waiting for some props to be updated and not the whole component.

EDIT: Ok I just leave my message and saw you update your post. So you did try componentWillReceiveProps already and give some more information. If you're waiting for some props or states to be changed probably you should try componentWillUpdate to see if they differ. I will check back later if you've some news :^).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM