2

I need to create global custom event, to be able listen and trigger it. In jQuery I would do it like that:

$(document).on('myCustomEvent', function(){
    console.log("myCustomEvent triggered");
})

$(document).trigger('myCustomEvent');

What for?

I need to create communication between not related components.


Question:

How to listen and trigger global custom events in ReactJS?

1 Answer 1

3

If you want to add listeners you can still do this with jQuery. Start listening in the componentDidMount and stop listening in componentWillUnmount see the docs. If it wants to be global (visible to child components), you can do this in the topmost component and propagate down with props.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.