ReactJS: Can I create my own SyntheticEvent?

I use ReactJS and SyntheticEvent to track the event, and it targets the DOM node.

I am creating several mutable components and I want them to run SyntheticEvent to track the DOM and track the changed value using e.target and e.target.value .

How can I create an instance of SyntheticEvent and assign them a DOM and its target value?

+7
source share
2 answers

In the current state, no, I looked for all the resources about SyntethicEvent to make sure that I did not find the API that was used to create the new SyntethicEvent, in any case you can try to use native addEventListener for the ref element.
Postscript If you have found an API that will provide a solution to your question, please share with us :)

0
source

in jsx that your component returns, you can add something like this

 <button onClick={this.myEventHandlerFunc}>button text</button> 

Declare the function myEventHandlerFunc: (e) {} in your component and do the following :)

Hope this helps ... Now I'm starting google to find additional examples;)

EDIT:

If you want to fire custom events, you can also just use jQuery, for example, and bind those of your events to the eventDidMount event of the life cycle. here you can do something like this:

 $(document).on('myCrazyEvent', this.componentEvtHandler) 

Elsewhere you can create and distribute an event like this

 $(document).trigger('myCrazyEvent', [customVar]); 
-1
source

Source: https://habr.com/ru/post/1207407/


All Articles