Is this a good solution?
https://github.com/immutable-js/immutable-js/wiki/Immutable-as-React-state
I need to put nested objects in my state. This solution seems simple enough in code, but I'm wondering if there are any reasons not to take this approach.
TL/DR: I recommend immutable for anything bigger than a simple news-list, together with redux-saga. But I would and don't use it for component's local state.
We have a big React app (JS, not TypeScript) that has all the state as Immutable objects.
The recommendation usually is the same as with redux and redux-saga: If you don't know why you need it, you probably don't. But here is the thing, if you start without it and later notice that it would be helpful, you are already deep in a mess and it's hard to switch over.
Immutable doesn't add obvious business value, but it reduces the chances of bugs and prevents you from doing things that might look good now but in the long run increase the cost of development. Especially when your dev team isn't an experienced bunch of seniors or generally tends to not strictly follow rules.
However, in my opinion, it is unecessary overkill for an components internal state, at least if business state is stored in your global redux state.
PS: Immutable is mature and probably doesn't need patches much, but it has to be said: Development of immutable essentially is stopped, they are discussing to bring in new people but who knows how that turns out. There are similar libraries which are more active
Related
Current State Management Stack: React, RTK/Redux, and Redux-Saga
Here's the high level issue. We have a complex flow and we don't have QA Engineering resources to be able to write the integration tests needed for this. This leads to the following issues:
A lot of context needed to change something in this flow(without introducing bugs)
Having to manually test a lot of different paths in the flow to try and make sure nothing was broken
A huge lack of efficiency
Even with dedicated QA effort, bugs still slip through to production with how many paths there are
I've been looking at xstate and really love the ability to generate statecharts(less context needed, non-technical stakeholder understanding, & seems like it would result in better efficiency moving forward). For state management, we currently use Redux across the platform(changing this would be incredibly out of scope). This leads me to my questions:
Does anyone know of a (well supported)npm package where I can integrate Redux with statecharts?
If not, what are the pros/cons of implementing both xstate and Redux? Xstate would only be for this flow and would need to pull some data from Redux. While Redux state is available to xstate, I'm assuming I wouldn't need to write it in a way that Redux is dependent on xstate.
I'm not super familiar with xstate but I don't want to dive too deep if this ends up being a waste of time/terrible idea.
ALSO - for context, this is a very large repo & the solution needs to be maintainable, readable, and most importantly - scalable
ALSO 2 - The main goal of this is the auto-generated statecharts. We have many hands in this flow and keeping documentation up to date manually is not feasible
You can always use statecharts as Redux reducer functions! In fact, one of the XState devs put together an example repo demonstrating exactly how to do that (as well as using XState services in a Redux middleware for side effects):
https://github.com/mattpocock/redux-xstate-poc
I know that Redux is a just option.
While thinking about whether to use Redux or not, I was looking for articles about cons and pros, but there were few recent articles. Old articles have content that I couldn't agree with.
The articles say that boilerplate code and performance are cons of Redux. but, Is it true even now?
Encapsulation
In redux structure, I could access any data(No encapsulation). but, I didn't. It depends on the developer's capabilities and I can care about encapsulation as much as I want.
Cohesion
When I used redux, My code had more cohesion. Data mutation logic is placed in the slice for every feature.
Boilerplate code
I indeed have to make code as the redux way in the Redux structure.
I had to write a little more code in Redux structure, but it was a little bit. Rather, more parts can be reused when using Redux.
When we make a controller in the back-end, we make code in a framework-dependent way. There is almost no one who makes the controller from very low levels because of the flexible design.
Performance
I have made views for some complex use-cases using react-redux. but, I could find meaningful performance down. I think it is also meaningless that there is a performance down due to hundreds of KB of Redux packages.
So my question is...
The articles about Redux cons I read were written 2 years ago. Using the Redux toolkit is a standard way now. Boilerplate code still is a con of Redux?
If the performance down is a con of Redux, Could you tell me specific examples? (What kind of project has performance problems when using redux, or the cases that don't use Redux because of performance.)
What is the biggest con of using Redux today? (Except that it's hard)
Any other thoughts or opinions, please let me know.
While thinking about whether to use Redux or not, I was looking for articles about cons and pros, but there were few recent articles
Different patterns and architectures don't have pros and cons in isolation, they only have pros-and-cons in comparison to some other architecture or pattern. So far you've only written about Redux - you need to compare it to something first.
The articles say that boilerplate code and performance are cons of Redux. but, Is it true even now?
Accusations of needing boilerplate code is not a criticism of Redux I'm familiar with. On the contrary, Redux actually reduces boilerplate compared to the older Flux pattern.
Encapsulation: In redux structure, I could access any data (No encapsulation). but, I didn't. It depends on the developer's capabilities and I can care about encapsulation as much as I want.
Blame JavaScript, not Redux. In JavaScript all objects are (generally) visible for the world to see: which I consider a strength because it makes scripts customizable and hackable, whereas trying to customize a third-party Java or .NET library (where object encapsulation is the norm) is very difficult if not impossible.
Being able to access all data in the state store is by-design. In Redux (and React) your state-store is meant to be a normalized representation of your application's data, so it makes sense for it to be entirely accessible. It doesn't make sense to arbitrarily restrict what data a component can read (it's not like you're running untrusted code).
Remember that state in Redux and React is immutable (i.e. you cannot edit the data in-place), so exposing everything doesn't introduce any risks because a misbehaving component cannot edit state in-place.
To be fair, you need to use Object.freeze to make the data truly immutable, which I imagine most people forget to do...
Encapsulation, as a property of a system's design, can be a good thing - and it can be a bad thing. Encapsulation generally makes sense when you need to hide internal implementation details that are orthogonal (or entirely unrelated) to the data that is being modelled, such as a Array<T>'s internal buffer pointers or a Map<K,V>'s hashtable buckets. But consider that in JavaScript those types (Array, Map, etc) are built-ins and you can use them to model your immutable state: you can't see into Map's buckets or Array's internal pointers, so you actually never stopped using encapsulated objects.
Cohesion: When I used redux, My code had more cohesion. Data mutation logic is placed in the slice for every feature.
I think you misunderstand what "cohesion" actually means in this context. I don't see how the fundamental design of Redux and its state-reducers relate to any concept of cohesion.
Boilerplate code: I indeed have to make code as the redux way in the Redux structure. I had to write a little more code in Redux structure, but it was a little bit. Rather, more parts can be reused when using Redux. When we make a controller in the back-end, we make code in a framework-dependent way. There is almost no one who makes the controller from very low levels because of the flexible design.
I cannot fully comprehend the above paragraph: the last couple of sentences have nothing to do with the rest of the text.
That said, I appreciate that Redux and React both require a fair bit of repetitive declarations for reducers, actions, and action-creators, but I wouldn't describe it as "Boilerplate" code because the information-theoretic content of those (repetitive) declarations is still very high.
Performance: I have made views for some complex use-cases using react-redux. but, I could find meaningful performance down. I think it is also meaningless that there is a performance down due to hundreds of KB of Redux packages.
The runtime performance of Redux is unrelated to the size of Redux libraries. You are conflating completely separate issues.
That said, I don't know where you're getting the idea that Redux requires you to have "hundreds of KB" of JS files because my last Redux project had a single redux.js file sized at 25KB, which was minified to redux.min.js which was only 6KB in size.
I assume you're referring to the #reduxjs/toolkit library (which has 210KB of source files, but the runtime redux-toolkit.umd.min.js is only 33KB.
Now there is something to be said about the performance cost of the Virtual DOM features in ReactJS, but ReactJS is not Redux. You're free to manipulate the DOM however you like when you use Redux directly - so this point is moot.
There is also a discussion to be had about the performance implications of having to clone immutable state compared to mutating state in-place, however immutable data has inherent qualities which mean you can safely clone-by-reference rather than cloning-by-value. And because Redux uses a directed (ideally acyclic) object-tree graph to represent immutable state it takes advantage of the fact that references to unchanged child objects can be safely passed to constructors of new immutable state (so, for example, if you have megabytes of data evenly-distributed throughout your normalized state graph, and your action and reducer only changes a single deeply-nested object property, then the only about log n data will be reallocated and copied, instead of the entire graph.
The articles about Redux cons I read were written 2 years ago. Using the Redux toolkit is a standard way now. Boilerplate code still is a con of Redux?
What boilerplate are you even talking about?
If the performance down is a con of Redux, Could you tell me specific examples? (What kind of project has performance problems when using redux, or the cases that don't use Redux because of performance.)
Think about it this way: JavaScript is far, far from being the fastest or most efficient programming language (e.g. the V8 JS engine will consume tens of megabytes of RAM just to run a simple "Hello, World" example script) - given that, I wouldn't worry too much about general performance in JS (...at least nothing beyond ensuring that any algorithms you implement in JS run in O(n log n) time or better).
What is the biggest con of using Redux today? (Except that it's hard)
I'd say the biggest disadvantage is having to put up with questions like that.
Any other thoughts or opinions, please let me know.
People use Redux because they want to ensure the data-flow through their JS code is consistent, predictable, and straightforward to reason about compared to ad-hoc JS scripts that don't conform to any overall general architecture or programming patterns. If you don't need those benefits then you might just be better-off doing writing ad-hoc JS.
I've been looking into Zustand and Recoil -- two state management libraries that are relatively new.
Recoil is heavily advertised as being "very performant" for React apps with deeply-nested structures. However, I don't see how (or how exactly) it can be superior to Zustand performance-wise (concurrent mode aside).
I could've gotten it wrong, but here's how I understood it from articles and talks:
The main reason "why" Recoil is performant is that any updates you make only trigger the relevant components to re-render, without bothering any other components. Recoil allows for it by design and out of the box, whereas Context-based libraries have to pass every change through the whole tree, have those changes diff'ed/reconciled and then probably only re-render what has to be changed.
Now, Zustand does not use the Context API at all. So, I would assume that (concurrent mode aside), it would have comparable performance benefits as Recoil, where Zustand would only "touch" the relevant components without ever piping the changes through the whole component tree.
Please let me know if I'm off in my understanding. Are these two libraries comparable in performance (without concurrent mode)? Or is there some other inherent property of Recoil's paradigm with atoms which makes it theoretically superior in terms of performance?
Please note: I would like the answer NOT to be influenced by patterns and practices too much. I understand that sometimes the biggest benefit of a paradigm can be in the "sound patterns it enforces", but I'm interested not in what they enforce but in what they allow to do with comparable effort.
For example, I understand that a flat, normalized state will allow for better performance in Zustand, and that Zustand does not necessarily "force you to do it in a proper/scalable way". But I wouldn't want the "optionality" of the right patterns to be a disadvantage.
I would like to understand more about the best practices for passing props around parents-child in react. the problem comes from having a standard way of doing this in a medium-large project to minimize confusion and technical debts such as performance optimization. So far, I only knew these methods on doing this:
standard prop drilling
Pros: easy
Cons: will become unmanageable in complex feature
utilizing React.Context
Pros: medium difficulty, naturally separate from main component
Cons: more time to write, may be unnecessary for smaller components, will do re renders that will cause performance issues headaches in, for example, large forms.
using global state from 'reactn' module
Pros: easy
Cons: will get unmanageable for large projects that have tons of components, cannot prevent rerender AFAIK.
using Redux
Pros: robust and compatible with firebase (react-redux-firebase, redux-firestore), can prevent rerender using areStatesEqual,
Cons: more boilerplates, more work to do for simpler state management
is there a guideline on standard practices on doing this? what do you people use for medium-larger projects? thanks!
The following answer is subjective!
There are basically 3 categories that I am aware of.
Keep everything local ( For any size of app which doesn't deal with sibling dependencies this will work fine. May cause some prop drilling, though it should be avoided as possible, it's not an anti-pattern. You come with better approaches to handle this i.e. PureComponent, React.memo, shouldComponentUpdate)
Keep everything global(redux/reactN/...) (have not seen this approach in my experience, not recommended in my opinion as requires you to hit central state every time when there is a change)
Keep a mix of both. (Highly seen this in medium to large scale projects, we use this approach with redux)
Choosing an option:
Personally speaking, irrespective of the size of an application one can start with the first approach and add the central state later when required(if you are not sure where to put states).
If you need to manage some state centrally from the beginning it can be added from start(if states are clearly defined).
If you choose any one of the above two approaches, to begin with, at one point in time you would need to decide either go for first-party i.e. React Context or a third party i.e. Redux, ....
You have already stated the pros and cons of these, you can compare and see which one outweighs the others.
For a simple answer from the list you provided I would suggest redux, writing actions and reducers does add a certain amount of overhead, and as react evolved and matured it now seems you can manage large applications w/o the use of redux: https://medium.com/#dan_abramov/you-might-not-need-redux-be46360cf367. But once you get how redux works you will be able to scale with ease. This is a good solution for react-redux beginner-intermediate level devs.
The best suggestion I have for you is to use react only.
useContext and useReducer will get you very far, but it requires an intermediate - experienced development skills. This approach will force you to invest time in data-structure, encapsulation of components and use of advance react patterns. (I would enroll to something like https://kentcdodds.com/workshops/advanced-react-patterns)
After doing much research I found out that Redux and Alt are two really popular flux implementations.
I also found that Alt is more of traditional flux pattern and Redux is slightly different from the traditional flux. In Redux, the state is stored as immutable tree which means there will be a new object created for every change.
Now coming to my question, I have a requirement to develop a WYSIWYG authoring platform. This means that the contents of this app will keep changing every few seconds.
So does this mean that using Redux for this app will be a bad option as there will be thousands of objects in the memory as we start editing the content. Is alt implementation a better option?
Which flux implementation makes more sense?
Thank you.
Most of the time you have 2 main areas where front end applications can take a significant performance hit. One is memory usage (as you pointed out) and the other is DOM rendering. As far as memory usage is concern, it would be hard to hit that performance limit, unless you have a memory leak or deal with extremely large data sets. I suspect that neither of these will be a problem for you. Instead DOM rendering and efficient updating should be the focus of your decision. Here you need to look at React more than Redux. Redux utilizes the render mechanisms of React components and this can be highly optimized when encountering performance issues. If you have faith in React performance (and in my opinion you should) then either framework would work in this case.
Here are a few friends :) having a good conversation on the topic (related to Redux): https://github.com/reactjs/redux/issues/634
I haven't seen any evidence that Redux's immutability paradigm is bad for performance. (I don't say that to be rude or dismissive, I would love to see the evidence if it exists.)
Frankly I don't think what you choose as your data layer is a major concern here. In a WYSIWYG app, the bulk of your complexity is going to be in the view layer.
My advice would be to make sure you write your app in a way that the data layer can easily be swapped out and replaced. That way, if you do find your chosen framework is a performance hog, you can easily replace it.
Since the overhead of the flux implementation is not an issue, the best choice would be the implementation that is the most logical to you. You are going to be more productive using tools that make sense to you.