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?
componentWillReceiveProps
orcomponentWillUpdate
is not a guarantee that thenextProps
differ from the current props. It's absolutely ok if they are the same. – Anatoliy Arkhipovthis.state
instead ofthis.props
? Maybe I'm misunderstanding the question, but the React guides recommend approaching props as if they are immutable, and instead recommend that you should be changing states. – Josh Beam