Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emitting custom event in React

In Vue.js we can emit custom events along with a parameter like

this.$emit('bark', 3);

and then we can listen to this event on the parent component like

<parent-component @bark=handleBark />

handleBark (howManyTimes) {
    console.log(howManyTimes);
    // logs 3
}

How can we do that in React?

like image 337
Pooria Han Avatar asked Nov 17 '20 13:11

Pooria Han


People also ask

How do you emit custom events in React?

addEventListener(eventName, listener); } function unsubscribe(eventName, listener) { document. removeEventListener(eventName, listener); } function publish(eventName, data) { const event = new CustomEvent(eventName, { detail: data }); document. dispatchEvent(event); } export { publish, subscribe, unsubscribe};

How do you emit event from parent to child in React?

To pass data from child to parent component in React: Pass a function as a prop to the Child component. Call the function in the Child component and pass the data as arguments. Access the data in the function in the Parent .

What is an event in React and how can you create it?

An event is an action that could be triggered as a result of the user action or system generated event. For example, a mouse click, loading of a web page, pressing a key, window resizes, and other interactions are called events.

Why we need Custom Event Emitter in react?

We need custom event emitter to do that. This pattern is being used by many well known libraries and a very good example is React Redux. An event emitter is a pattern that listens to a named event, fires a callback, then emits that event with a value. Sometimes this is referred to as event dispatcher or pub/sub model, or listener.

How to perform actions based on user events in react?

Just like HTML, React can perform actions based on user events. React has the same events as HTML: click, change, mouseover etc. onClick instead of onclick. onClick= {shoot} instead of onClick="shoot ()".

How do I add an event listener to a React component?

When using React you should generally not need to call addEventListener to add listeners to a DOM element after it is created. Instead, just provide a listener when the element is initially rendered. When you define a component using an ES6 class, a common pattern is for an event handler to be a method on the class.

How does the useeffect hook work in React Native?

Our useEffect hook only runs once on component mount, and adds an event listener to the window resize event. The event listener sets our state variable to the new size of the viewport. Finally, we return a function to be called on unmount which will tidy up and remove the event listener.


1 Answers

You just simply pass down the custom event handler as props.

For example if you have Parent and Child functional components. You can then define the custom event handler in the Parent component like:

function Parent(props) {
  const handleBark = (howManyTimes) => {
    console.log(howManyTimes);
  };
  
  // note below I am sending the handleBark method to Child as props
  return (<Child bark={handleBark} />);
}

and then inside the Child component you can simply call it as:

props.bark(10);
like image 166
usafder Avatar answered Nov 09 '22 23:11

usafder