useReceiver

This hook added receiver to eventrix in component. When component did mount receiver is added to eventrix. When component will unmount receiver will be removed from eventrix.

If You want use hooks, hocs or component decorator You must use EventrixProvider in Your application. Go to "EventrixProvider" page in "Context" section for more details.

import React from 'react';
import axios from 'axios';
import { useReceiver, useEmit } from 'eventrix';

const DeleteUserButton = ({ user }) => {
    const emit = useEmit();
    useReceiver(
        'users:remove',
        (eventName, userData, stateManager) => {
            return axios.delete(`https://some.com/${userData.id}`).then(() => {
                const users = stateManager.getState('users');
                const newUsersList = state.users.filter(item => item.id !== userData.id);
                stateManager.setState('users', newUsersList);
            });
        }
    );
    
    const removeUser = useCallback(() => {
        emit('users:remove', user);
    }, [emit, user])
    
    return (
        <button onClick={removeUser}>
            Remove user
        </button>
    );
}

Last updated