score:48

Accepted answer

i haven't used angular, but reading the link above, it seems that you're trying to code for something that you don't need to handle. you make changes to state in your react component hierarchy (via this.setstate()) and react will cause your component to be re-rendered (effectively 'listening' for changes). if you want to 'listen' from another component in your hierarchy then you have two options:

  1. pass handlers down (via props) from a common parent and have them update the parent's state, causing the hierarchy below the parent to be re-rendered.
  2. alternatively, to avoid an explosion of handlers cascading down the hierarchy, you should look at the flux pattern, which moves your state into data stores and allows components to watch them for changes. the fluxxor plugin is very useful for managing this.

score:1

it's been a while but for future reference: the method shouldcomponentupdate() can be used.

an update can be caused by changes to props or state. these methods are called in the following order when a component is being re-rendered:

static getderivedstatefromprops() 
shouldcomponentupdate() 
render()
getsnapshotbeforeupdate() 
componentdidupdate()

ref: https://reactjs.org/docs/react-component.html

score:4

using usestate with useeffect as described above is absolutely correct way. but if getsearchresults function returns subscription then useeffect should return a function which will be responsible for unsubscribing the subscription . returned function from useeffect will run before each change to dependency(name in above case) and on component destroy

score:11

if you use hooks like const [ name , setname ] = usestate (' '), you can try the following:

 useeffect(() => {
      console.log('listening: ', name);
    }, [name]);

score:30

i think you should be using below component lifecycle as if you have an input property which on update needs to trigger your component update then this is the best place to do it as its will be called before render you even can do update component state to be reflected on the view.

componentwillreceiveprops: function(nextprops) {
  this.setstate({
    likesincreasing: nextprops.likecount > this.props.likecount
  });
}

score:32

since react 16.8 in 2019 with usestate and useeffect hooks, following are now equivalent (in simple cases):

angularjs:

$scope.name = 'misko'
$scope.$watch('name', getsearchresults)

<input ng-model="name" />

react:

const [name, setname] = usestate('misko')
useeffect(getsearchresults, [name])

<input value={name} onchange={e => setname(e.target.value)} />

score:49

in 2020 you can listen state changes with useeffect hook like this

export function mycomponent(props) {
    const [mystate, setmystate] = usestate('initialstate')

    useeffect(() => {
        console.log(mystate, '- has changed')
    },[mystate]) // <-- here put the parameter to listen
}

score:365

the following lifecycle methods will be called when state changes. you can use the provided arguments and the current state to determine if something meaningful changed.

componentwillupdate(object nextprops, object nextstate)
componentdidupdate(object prevprops, object prevstate)

Related Query

More Query from same tag