Getting started

Getting Started with Eventrix

Eventrix is events and state management system which gives you tools to connect your UI with JS bussines logic. Component can send events to another components or JS bussines logic which give you posibility to send some XHR to get data from server. Eventrix also give you posibility to store some data in eventrix state. You can share this state with Your components and JS bussines logic part.

Events management

Eventrix class instance is something like store in Redux but not the same.

/eventrix/index.js
import { Eventrix } from 'eventrix';

const initialState = { users: [] };
const eventsReceiver = [];

const eventrix = new Eventrix(initialState, eventsReceiver);

Eventrix class receive initial state and events receivers to handle events and to do someting with event data. Eventrix instance has events managament system and state manager inside. When You emit some event by

eventrix.emit('createUserEventName', userData)

then all listeners and events receivers that listen on it will be invoked.

/eventrix/index.js
const createUserListener = (userData) => { // do something with userData };
eventrix.listen('createUserEventName', createUserListener);

eventrix.emit('createUserEventName', { name: 'Max' });
// after call emit createUserListener will be called with { name: 'Max' } as userData

State management

Now we know how to listen on events and emit events. But what about state? Where is it? If we want to change state, we need to use events receiver

If you use EventsReceiver in eventrix instance, it will get access to stateManager when event will be called. State manager can set new state or get any state which you need.

Eventrix has one default events receiver on event setState. When you emit this event with

it will set state users to

Fetch with Eventrix

If you want to use fetch or axios with eventrix you can do this with EventsReceiver and emit

When you want to fetch some data and put it to state, you can also use special helper function called fetchToStateReceiver

Hmmm what is the difference ? You define state path and promise must return new state. Take a look on more complex example with users manage class.

Fetch error handling with Eventrix

You can handle error using catch on promise as we do in fetch with eventrix section or use fetchHandler. Fetch handler is small midlerware for fetch method that emit events when fetch success or fetch error.

If you want create event data basing on response, error, or receiver event data you can use getData attribute on success and error.

React

Now we know how to send events, manage state and send requests to server with Eventrix. What about React and components? Eventrix has react section which gives us react context, provider, hocs and hooks.

Context

Hooks

HOCs

If you want to use Eventrix in Your react project you must use EventrixProvider on the top of the project components tree.

When we have EventrixProvider all of HOCs and hooks can be used in project.

HOCs

Eventrix has two HOCs withEventrix and withEventrixState. First HOC pass eventrix instance to props.

Second HOC give you posibility to listen on selected state change and pass this state to props. Component will be rerender when state will be changed.

If you want to map state to props You can use third argument mapStateToProps.

What can I do when I need data from props to define stateNames? You can pass function as a second argument.

Hooks

Eventrix gives us special react hooks for state and events management. For example if You want to rerender component when some state will be changed, you can use useEventrixState hook.

useEventrixState hook also give you posibility to update used state. This hook is similar to useState but don’t have initial value. Remember, this is eventrix state and you can share this state with other component in Your app.

These two components use the same state

When we need to listen on some event and do some action, we can use useEvent hook.

This example showed us two situations:

  • displaying loader when users list is loading

  • removing loader when users list is loaded successfully.

Sometimes we need to emit some events from components. For this purpose Eventrix has useEmit hook that gives you access to eventrix.emit method.

Last updated

Was this helpful?