Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redux Alternative [closed]

I have recently started learning React and I quickly encountered the problem of a bloated parent component full of functions and state. At first I looked at Flux and then I looked at Redux but both seemed like really overbearing solutions.

I am wondering why something like this is not done:

//EventPropagator.js
let EventPropagator = {
    registerListener(listenerObject){
        if (this.listeners == null)
            this.listeners = []
        this.listeners.push(listenerObject)
    },
    fireEvent(eventObject){
        let listenerList = this.listeners.filter(function(listener){
            return listener.eventType === eventObject.eventType
        })
        listenerList.forEach((listener) => {
            listener.callback(eventObject.payload)
        })
    }
}
export default EventPropagator

To use: components simply registerListener and fireEvent:

//main.js
import EventPropagator from './events/EventPropagator'

EventPropagator.registerListener({
    "eventType": "carry_coconut",
    "callback": (payload) => {
        // actually carry coconut
        this.setState({"swallow_type": payload.swallow})
        console.log(payload)
    }
})
EventPropagator.fireEvent({
    "eventType": "carry_coconut",
    "payload": { "swallow": "african" }
})

This would allow a lot of decoupling and state could easily be passed around with this sort of event. What are the downsides to this approach?

like image 394
jcuenod Avatar asked Nov 09 '16 23:11

jcuenod


People also ask

What can I use instead of Redux?

The simplest alternative to Redux is to use React's built in Contexts functionality. One of the many important pieces of functionality state management libraries provide is the ability to access content across multiple distant components.

Do I need Redux 2022?

Well, that's actually a good question to be asking. The short answer is no, Redux is not necessary for React applications. However, there are always clear warning signs when you should be using a state management library like Redux and signs that you shouldn't.

Is MobX better than Redux?

Debug process: Compared to MobX, debugging in Redux is a better experience because it has more developer tools and less abstraction. The Redux becomes more predictable with the flux paradigm. Debugging in MobX is much more difficult due to increased abstraction and average developer tools.

Is Redux deprecated?

The redux core package still works, but today we consider it to be obsolete. All of its APIs are also re-exported from @reduxjs/toolkit , and configureStore does everything createStore does but with better default behavior and configurability.


3 Answers

You should try mobX

mobX is a state management library which took the advantage of decorators and succeeded in removing unwanted code. For example, there is no concept of reducers in mobx.

Hope this helps!

like image 127
Pranesh Ravi Avatar answered Sep 19 '22 00:09

Pranesh Ravi


There is Reactn based in hooks.
It is orders of magnitude easier to use, compared to Redux. Check https://www.npmjs.com/package/reactn and read Stover's blog.

like image 26
Juan Lanus Avatar answered Sep 19 '22 00:09

Juan Lanus


Redux is a continuation of React's declarative programming Style. A simple way of mapping your application logic to components is to use something like Backbone.React.Component but using redux, will let you use all the tooling & middle-ware. Also have indefinite transitions in your apps makes debugging a lot easier. Now, I agree that you need a lot of wiring & boilerplate to get anyway.

If you want to use redux, you could look at something like redux-auto

Redux-auto: Redux made easy (with a plug and play approach)

Removing the boilerplate code in setting up a store & actions. why? This utility to allow you to get up and running with Rudux in a fraction of the time!

You can see now a redux-auto: helloworld project

like image 35
codemeasandwich Avatar answered Sep 19 '22 00:09

codemeasandwich