19

I'd like to "fire an event" in one component, and let other components "subscribe" to that event and do some work in React.

For example, here is a typical React project.

I have a model, fetch data from server and several components are rendered with that data.

interface Model {
   id: number;
   value: number;
}

const [data, setData] = useState<Model[]>([]);
useEffect(() => {
   fetchDataFromServer().then((resp) => setData(resp.data));
}, []);

<Root>
   <TopTab>
     <Text>Model with large value count:  {data.filter(m => m.value > 5).length}</Text>
   </TobTab>
   <Content>
      <View>
         {data.map(itemData: model, index: number) => (
            <Item key={itemData.id} itemData={itemData} />
         )}
      </View>
   </Content>
   <BottomTab data={data} />
</Root>

In one child component, a model can be edited and saved.

const [editItem, setEditItem] = useState<Model|null>(null);
<Root>
   <TopTab>
     <Text>Model with large value count:  {data.filter(m => m.value > 5).length}</Text>
   </TobTab>
   <ListScreen>
      {data.map(itemData: model, index: number) => (
          <Item 
             key={itemData.id} 
             itemData={itemData} 
             onClick={() => setEditItem(itemData)}
          />
      )}
   </ListScreen>
   {!!editItem && (
       <EditScreen itemData={editItem} />
   )}
   <BottomTab data={data} />
</Root>

Let's assume it's EditScreen:

const [model, setModel] = useState(props.itemData);

<Input 
   value={model.value}
   onChange={(value) => setModel({...model, Number(value)})}
/>
<Button 
   onClick={() => {
       callSaveApi(model).then((resp) => {
           setModel(resp.data);
           // let other components know that this model is updated
       })
   }}
/>

App must let TopTab, BottomTab and ListScreen component to update data

  • without calling API from server again (because EditScreen.updateData already fetched updated data from server) and
  • not passing updateData function as props (because in most real cases, components structure is too complex to pass all functions as props)

In order to solve above problem effectively, I'd like to fire an event (e.g. "model-update") with an argument (changed model) and let other components subscribe to that event and change their data, e.g.:

// in EditScreen
updateData().then(resp => {
   const newModel = resp.data;
   setModel(newModel);
   Event.emit("model-updated", newModel);
});

// in any other components
useEffect(() => {
   // subscribe model change event
   Event.on("model-updated", (newModel) => {
      doSomething(newModel);
   });
   // unsubscribe events on destroy
   return () => {
     Event.off("model-updated");
   }
}, []);

// in another component
useEffect(() => {
   // subscribe model change event
   Event.on("model-updated", (newModel) => {
      doSomethingDifferent(newModel);
   });
   // unsubscribe events on destroy
   return () => {
     Event.off("model-updated");
   }
}, []);

Is it possible using React hooks?

How to implement event-driven approach in React hooks?

glinda93
  • 7,659
  • 5
  • 40
  • 78
  • 1
    well, you can solve this problem using `redux`. and you can use 'useSelector' hook. According to the [docs](https://react-redux.js.org/api/hooks#useselector) useSelector() will also subscribe to the Redux store, and run your selector whenever an action is dispatched. – Naresh Jul 10 '20 at 04:45
  • @Naresh I don't know... is it event-driven? Can you show some example? – glinda93 Jul 10 '20 at 05:10
  • Redux uses actions in reducers, which is similar to events, but pure functional. You can also get something similar in plain React using `useReducer` but it's not as powerful. Finally you could just synthetic event handlers, which is most typical in React. – Nick McCurdy Jul 10 '20 at 05:19
  • 1
    https://codesandbox.io/s/9on71rvnyo?file=/src/components/Todo.js take a look at this. it's not completely implemented using hooks but you can update it by following this [docs](https://react-redux.js.org/api/hooks) – Naresh Jul 10 '20 at 05:40

5 Answers5

11

There cannot be an alternative of event emitter because React hooks and use context is dependent on dom tree depth and have limited scope.

Is using EventEmitter with React (or React Native) considered to be a good practice?

A: Yes it is a good to approach when there is component deep in dom tree

I'm seeking event-driven approach in React. I'm happy with my solution now but can I achieve the same thing with React hooks?

A: If you are referring to component state, then hooks will not help you share it between components. Component state is local to the component. If your state lives in context, then useContext hook would be helpful. For useContext we have to implement full context API with MyContext.Provider and MyContext.Consumer and have to wrap inside high order (HOC) component Ref

so event emitter is best.

In react native, you can use react-native-event-listeners package

yarn add react-native-event-listeners

SENDER COMPONENT

import { EventRegister } from 'react-native-event-listeners'

const Sender = (props) => (
    <TouchableHighlight
        onPress={() => {
            EventRegister.emit('myCustomEvent', 'it works!!!')
        })
    ><Text>Send Event</Text></TouchableHighlight>
)

RECEIVER COMPONENT

class Receiver extends PureComponent {
    constructor(props) {
        super(props)
        
        this.state = {
            data: 'no data',
        }
    }
    
    componentWillMount() {
        this.listener = EventRegister.addEventListener('myCustomEvent', (data) => {
            this.setState({
                data,
            })
        })
    }
    
    componentWillUnmount() {
        EventRegister.removeEventListener(this.listener)
    }
    
    render() {
        return <Text>{this.state.data}</Text>
    }
}
Muhammad Numan
  • 23,222
  • 6
  • 63
  • 80
  • React Native has built-in EventEmitter: https://stackoverflow.com/questions/36774540/eventemitter-and-subscriber-es6-syntax-with-react-native, https://github.com/facebook/react-native/issues/27413#issuecomment-572621322 – glinda93 Jul 14 '20 at 07:23
  • const Event = new EventEmitter(); wont work in react native but it work in react js. for expo or react native you can use this packagef – Muhammad Numan Jul 14 '20 at 07:28
  • `import EventEmitter from 'react-native/Libraries/vendor/emitter/EventEmitter'; const event = new EventEmitter();` - working code in my RN 0.62.2 project, although it shows declaration file missing warning in typescript – glinda93 Jul 14 '20 at 07:42
  • react-native-event-listeners this package is working on all the versions of react native and expo – Muhammad Numan Jul 14 '20 at 08:25
  • @bravemaster I think my answer support all the version of react-native what do you think? – Muhammad Numan Jul 18 '20 at 07:25
  • I was asking if I can achieve the same effect using React hooks, not how to use EventEmitter – glinda93 Jul 18 '20 at 08:09
  • @bravemaster you have edited your question after my answer. react-native did not provide any method before. the only way is to use event emitter. – Muhammad Numan Jul 18 '20 at 09:39
  • I've asked my question clearly in both OP and bounty description. And I already added a link to a react native solution and similar approach in my answer before yours. – glinda93 Jul 18 '20 at 10:08
  • yeah, it is not possible without event emitter. – Muhammad Numan Jul 18 '20 at 10:21
  • @bravemaster this bounty will be spoil because there is no other solution than EventEmitter. – Muhammad Numan Jul 18 '20 at 13:26
  • Your post does not answer my question. I know you got an idea but prove it in your post. Anyway, you will get half of the bounty, I don't mind the other half as long as I don't have a sufficient answer. – glinda93 Jul 18 '20 at 13:28
  • Thanks but can you explain about scope in second answer in detail? I don't understand it. – glinda93 Jul 18 '20 at 13:41
  • *useContext would be useful. so event emitter is best* - that doesn't make any sense – glinda93 Jul 18 '20 at 13:57
  • useContext or React hooks were two things I'd like to know whether could be an alternative approach to EventEmitter – glinda93 Jul 18 '20 at 13:59
  • 1
    for useContext we have to implement full context API with `MyContext.Provider` and `MyContext.Consumer` and have to wrap inside high order component – Muhammad Numan Jul 18 '20 at 13:59
  • You can improve your post instead of commenting. – glinda93 Jul 18 '20 at 14:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218095/discussion-between-muhammad-numan-and-bravemaster). – Muhammad Numan Jul 18 '20 at 14:08
  • @bravemaster why you did not accept my answer? – Muhammad Numan Apr 16 '21 at 05:02
  • This is why I still prefer Vue for most apps. It does have pitfalls for highly custom and/or larger scaled apps, but I think its API encompasses the important points of modern web development and implements them in an intuitive way. – GHOST-34 Nov 09 '21 at 18:14
8

Not sure why the EventEmitter has been downvoted, but here's my take:

When it comes to state management, I believe using a Flux-based approach is usually the way to go (Context/Redux and friends are all great). That said, I really don't see why an event-based approach would pose any problem - JS is event based and React is just a library after all, not even a framework, and I can't see why we would be forced to stay within its guidelines.

If your UI needs to know about the general state of your app and react to it, use reducers, update your store, then use Context/Redux/Flux/whatever - if you simply need to react to specific events, use an EventEmitter.

Using an EventEmitter will allow you to communicate between React and other libraries, e.g. a canvas (if you're not using React Three Fiber, I dare you to try and talk with ThreeJS/WebGL without events) without all the boilerplate. There are many cases where using Context is a nightmare, and we shouldn't feel restricted by React's API.

If it works for you, and it's scalable, just do it.

EDIT: here's an example using eventemitter3:

./emitter.ts

import EventEmitter from 'eventemitter3';

const eventEmitter = new EventEmitter();

const Emitter = {
  on: (event, fn) => eventEmitter.on(event, fn),
  once: (event, fn) => eventEmitter.once(event, fn),
  off: (event, fn) => eventEmitter.off(event, fn),
  emit: (event, payload) => eventEmitter.emit(event, payload)
}

Object.freeze(Emitter);

export default Emitter;

./some-component.ts

import Emitter from '.emitter';

export const SomeComponent = () => {
  useEffect(() => {
    // you can also use `.once()` to only trigger it ... once
    Emitter.on('SOME_EVENT', () => do what you want here)
    return () => {
      Emitter.off('SOME_EVENT')
    }
  })
}

From there you trigger events wherever you want, subscribe to them, and act on it, pass some data around, do whatever you want really.

Joel Beaudon
  • 113
  • 1
  • 7
  • I learned that EventEmitters doesn't work well with React states, esp. when states are objects. You can use createRef for them to avoid that problem though. – glinda93 Oct 30 '20 at 13:11
  • of course, because state is asynchronous. My point is take sometimes you want to communicate **events** without data attached to it, or very little data, which can be useful when working on more "creative" apps. But if you do need to exchange data while using EventEmitter, refs are the way to go. – Joel Beaudon Dec 17 '20 at 14:03
  • 2
    Simple and straightforward. Much easier than Redux and Context. I don't know why not using this approach everywhere. Just wondering why creating a new object and freezing it. Exporting eventEmitter works fine too. – Eduardo May 12 '21 at 22:08
  • I like to freeze exported objects to prevent them from being manipulated. Although a deepFreeze might be more useful :) – Joel Beaudon May 14 '21 at 10:59
  • 3
    The global event bus pattern feels like cheating-- sure. But why then does cheating feel so good? It's because the flux pattern is like religous orthodoxy and you're a deviant; and you're beatutiful, just the way you are. But seriously, in an app like Figma, you're eventually going to need to communicate with components with whom you don't share state. If it feels good, do it. (Pssst.. it feels good) https://www.pluralsight.com/guides/how-to-communicate-between-independent-components-in-reactjs – MeatFlavourDev Jul 03 '21 at 12:53
3

We had a similar problem and took inspiration from useSWR.

Here is a simplified version of what we implemented:

const events = [];
const callbacks = {};

function useForceUpdate() {
   const [, setState] = useState(null);
   return useCallback(() => setState({}), []);
}

function useEvents() {

    const forceUpdate = useForceUpdate();
    const runCallbacks = (callbackList, data) => {
       if (callbackList) {
          callbackList.forEach(cb => cb(data));
          forceUpdate();
       }
     
    }

    const dispatch = (event, data) => {
        events.push({ event, data, created: Date.now() });
        runCallbacks(callbacks[event], data);
    }

    const on = (event, cb) => {
        if (callbacks[event]) {
           callbacks[event].push(cb);
        } else {
          callbacks[event] = [cb];
        }

        // Return a cleanup function to unbind event
        return () => callbacks[event] = callbacks[event].filter(i => i !== cb);
    }

    return { dispatch, on, events };
}

In a component we do:

const { dispatch, on, events } = useEvents();

useEffect(() => on('MyEvent', (data) => { ...do something...}));

This works nicely for a few reasons:

  1. Unlike the window Event system, event data can be any kind of object. This saves having to stringify payloads and what not. It also means there is no chance of collision with any built-in browser events
  2. The global cache (idea borrowed from SWR) means we can just useEvents wherever needed without having to pass the event list & dispatch/subscribe functions down component trees, or mess around with react context.
  3. It is trivial to save the events to local storage, or replay/rewind them

The one headache we have is the use of the forceUpdate every time an event is dispatched means every component receiving the event list is re-rendered, even if they are not subscribed to that particular event. This is an issue in complex views. We are actively looking for solutions to this...

jramm
  • 6,415
  • 4
  • 34
  • 73
0

You can create use context in App.js using useContext, and then in you child component you can use values from it and update the context as soon as the context get updated it will update the values being used in other child component, no need to pass props.

Neelam Soni
  • 1,251
  • 8
  • 15
0

You can achieve this with any React's global state management.

In your store, have a useEffect for your event subscription, and a reducer for each of your event.

If you have 2 data sources, the subscription and the query, then initialize your state values with your query, then listen to the subscription.

Something like this

const reducer = (state, action) => {
   switch(action.type) {
     case 'SUBSCRIBE':
        return action.payload
     default:
        return state
   }
}

Assuming you are using https://github.com/dai-shi/use-reducer-async

const asyncActions = {
   QUERY: ({ dispatch }) => async(action) => {
     const data = await fetch(...)
     dispatch({ type: 'query', payload: data })
   }
}

You can also use middleware in Redux

const [state, dispatch] = useReducer(reducer, initialValues, asyncActions)

useEffect(() => {
   dispatch({ type: 'QUERY' })
   Event.on((data) => {
      dispatch({ type: 'SUBSCRIBE', payload: data })
   })
   return () => Event.off()
}, [])

return <Provider value={state}>{children}</Provider>
Archmad
  • 11
  • 3