Redux -> Eventrix

This section provides information on how to transition from Redux to Eventrix

The transition from redux to eventrix is ​​not that hard. Some things are named differently, but they work similarly. The following examples show what the code looks like using Redux and Eventrix.

Store

Initializing store and core elements is almost the same. Eventrix doesn't need middlewares unlike redux. Eventrix can handle asynchronous actions itself.

store.js
import { createStore, applyMiddleware } from "redux";
import thunk from 'redux-thunk'
import reducers from "./reducers";

export default createStore(reducers, applyMiddleware(thunk));

Reducer

Instead of the reducer in eventrix, it is receiver. It differs significantly from the reducer. Receiver is responsible for receiving the event and has the ability to modify the state using stateManager. StateManager has access to the entire state, so it can download and modify any state. If you need to get some data from API, you can do it in receiver and return promise.

reducers.js
import { combineReducers } from "redux";

const counter = (state = [], action) => {
    switch (action.type) {
        case `increaseCounter`:
            return counter + 1;
        case `reduceCounter`:
            return counter - 1;
        default:
            return state;
    }
};

export default combineReducers({ counter });

Provider

Provider does not differ only in names.

App.jsx
import React from "react";
import { Provider } from "react-redux";
import store from "./store";
import Toolbar from "./components/Toolbar";
import Counter from "./components/Counter";

export default function App() {
    return (
        <Provider store={store}>
            <div>
                <Counter />
                <Toolbar/>
            </div>
        </Provider>
    );
}

Dispatch

Redux handles the actions we pass to dispatch. Eventrix has events that are emitted by emit. Events, unlike actions, can be received by components if they register to them. This is how we enable communication between components. Method emit returns a promise waiting for all async receivers and listeners to be called. Thanks to this, you can handle the end of an event in the component.

components/Toolbar.jsx
import React, { useCallback } from "react";
import { useDispatch } from "react-redux";

const Toolbar = () => {
    const dispatch = useDispatch();
    const increaseCounter = useCallback(() => {
        dispatch({ type: 'increaseCounter' });
    }, [dispatch]);
    return (
        <button onClick={increaseCounter}>Increase counter</button>
    );
};

export default Toolbar;

State

Redux handles state change in components with useSelector. Eventrix handles this with useEventrixState. The main difference is in use and action. In terms of operation, selectors are called whenever dispach is called and redux checks if the component should rerender. Eventrix rerender component only when the change relates to the state for which the component has registered.

components/Counter.jsx
import React from "react";
import { useSelector } from "react-redux";

const counterSelector = (state) => {
    return state.counter;
};

const Counter = () => {
    const counter = useSelector(counterSelector);

    return <div>{counter}</div>;
};

export default Counter;

Last updated