Skip to content

Commit

Permalink
Merge pull request #16 from rstgroup/fix/useReceiver--removeDependencies
Browse files Browse the repository at this point in the history
fix(#15): remove dependencies from useReceiver
  • Loading branch information
mprzodala authored Apr 29, 2022
2 parents 7c38e70 + 92d2a0b commit 72d5f7d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
14 changes: 5 additions & 9 deletions src/react/hooks/useReceiver.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@ import useReceiver from './useReceiver';
describe('useReceiver', () => {
const ItemComponent = () => {
const emit = useEmit();
useReceiver<string>(
'testEvent',
(eventName, eventData, stateManager) => {
act(() => {
stateManager.setState('foo', eventData);
});
},
[],
);
useReceiver<string>('testEvent', (eventName, eventData, stateManager) => {
act(() => {
stateManager.setState('foo', eventData);
});
});
useEvent('remoteFetch', () => {
act(() => {
emit('testEvent', 'testData').then(() => {
Expand Down
13 changes: 4 additions & 9 deletions src/react/hooks/useReceiver.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import React, { useContext, useEffect, useCallback } from 'react';
import { useContext, useEffect } from 'react';
import { EventrixContext } from '../context';
import { ReceiverI } from '../../interfaces';
import EventsReceiver from '../../EventsReceiver';

function useReceiver<EventDataI>(
eventName: string,
receiverMethod: ReceiverI<EventDataI>,
receiverDependenciesList: React.DependencyList,
): void {
function useReceiver<EventDataI>(eventName: string, receiverMethod: ReceiverI<EventDataI>): void {
const { eventrix } = useContext(EventrixContext);
const receiverCallback = useCallback(receiverMethod, receiverDependenciesList);

useEffect(() => {
const receiver = new EventsReceiver(eventName, receiverCallback);
const receiver = new EventsReceiver(eventName, receiverMethod);
eventrix.useReceiver(receiver);
return () => {
eventrix.removeReceiver(receiver);
};
}, [eventName, receiverCallback]);
}, [eventName, receiverMethod]);
}

export default useReceiver;

0 comments on commit 72d5f7d

Please sign in to comment.