Search code examples
reactjstypescriptreact-lifecycle

Run componentDidMount after a component attribute value change


I don't know whether this is possible in React or not as I am a novice React developer. What I want is to call a function when the attribute (data-link) of my input element changes. I use componentDidUpdate but it triggers whenever any component changes.

I just want to call componentDidUpdate whenever there is any change in the specific attribute of a specific element.


Solution

  • I think the best option is to check if the specific value is changed __(the one you want to call the function on change), you can use

    if (prevState.attribute !== this.state.attribute) {()=>callFunction}

    then the callFunction is only called when there is a change in the attribute state.