Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ES6 Classes with Redux

I'm working on a project that brings in a ton of data from one endpoint into a a single reducer. I'd like to convert that data in ES6 Classes, so I can give them helper method, provide relations between the data, and not have to work with plain javascript objects all the time. Also, to get relations between the data, I'm having to do n-squared computations and that's slowing down the frontend.

Here are the options I'm seeing:

1) Create a selector that connects with the redux store. This selector could get the data from the reducer and convert it into multiple ES6 classes that I've defined. If the reducer gets new data that is different, then the selector will recreate the ES6 class instantiations.

2) https://github.com/tommikaikkonen/redux-orm This seems fantastic as well.

3) Create multiple selectors on the data set to that will compute a specified relation in the data set, so I can just call that selector each time I want to get a relation that would otherwise be an n-squared computation to get.

My question is which route of the three should I take? Is there an alternative besides these 3? Or do people just work with javascript objects mostly on the frontend and not deal with ES6 classes.


Update:

Two months later, and I'm still using Redux-ORM in production and it is fantastic! Highly recommend.

like image 315
NateW Avatar asked Dec 13 '16 23:12

NateW


People also ask

Can we use react redux with class component?

Yes, it is recommended to use functional components with redux , and there is a way to have a local state in a functional component.

Can we use Redux with JavaScript?

Redux is a predictable state container designed to help you write JavaScript apps that behave consistently across client, server, and native environments and are easy to test. While it's mostly used as a state management tool with React, you can use it with any other JavaScript framework or library.

What are subscribers in Redux?

In Redux, subscriptions are called after the root reducer has returned the new state, so you may dispatch in the subscription listeners. You are only disallowed to dispatch inside the reducers because they must have no side effects.

How do I use redux in JavaScript?

You can create your store in one file and export it. import { createStore } from 'redux' import reducers from "./reducers"; const store = createStore(reducers) export default store; Now you can import store from any other file and use it. Super helpful.


1 Answers

It's certainly entirely possible to do all that handling with "plain" functions and selectors. There's info on normalization in the Redux FAQ, and I have some articles on selectors and normalization as part of my React/Redux links list.

That said, I am a huge proponent of Redux-ORM. It's a fantastic tool for helping manage normalized/relational data in your Redux store. I use it for normalizing nested data, querying data, and updating that data immutably.

My Practical Redux blog post series includes two articles talking about Redux-ORM specifically: Redux-ORM Basics and Redux-ORM Concepts and Techniques. The latest post, Practical Redux Part 5: Loading and Displaying Data, shows Redux-ORM in action as well.

The author of Redux-ORM, Tommi Kaikkonen, actually just put up a beta of a major update to Redux-ORM that improves the API and behavior, which I'm looking forward to playing with.

I definitely recommend it!

like image 200
markerikson Avatar answered Oct 23 '22 17:10

markerikson