import axios from 'axios';
import { EventsReceiver } from 'eventrix';
const fetchUsersReceiver = new EventsReceiver('fetchUsers', (eventName, eventData, stateManager) => {
return axios.get('https://myDomain.com/users').then(({ data: usersList }) => {
stateManager.setState('users', usersList);
});
});
export default fetchUsersReceiver;
eventrix.useReceiver(fetchUsersReceiver)
/components/UsersList.jsx
import React, { useEffect } from 'react';
import { useEventrixState, useEmit } from 'eventrix';
const UsersList = () => {
const [users] = useEventrixState('users');
const emit = useEmit();
useEffect(() => {
emit('fetchUsers');
}, [emit])
return (
<div>
{users.map(user => <div key={user.id}>{user.name} {user.surname}</div>)}
</div>
);
}
export default UsersList;