4 ms·
Redux and MobX are overkill. What most people want is an easy way to pass down state to nested components, but the context API is a bit verbose and otherwise y
by k__ 9y ago
Redux and MobX are overkill.
What most people want is an easy way to pass down state to nested components, but the context API is a bit verbose and otherwise you end up with the props-drilling problem, passing and passing and passing.
Problem is, Redux and MobX solve far more than this problem and in the end you are stuck with all the complexity they add, even if you don't need the other benefits they bring.
In 80% of all apps you don't need much state. They don't have deeply nested components so you can simply store the state in your root components (Screen/Page) via setState and pass everything down via props.
If you end up with deeply nested probs, use "unstated", it adds a bit better usability to the context API and lets you avoid the props-drilling problem. You basically get something like the "connect" from Redux, without the rest Redux would require.
If you somehow end up with an app that fits in the 1% who need fully predictable control over their global state, go for Redux.
React is the simplest framework I've ever seen, sadly Redux and co made it much more complex than needed to be, which drove many people away from it.
- lilbobbytables 9y agoI've used it primarily for manaing logged in user info. It's one of the few bits that is useful globally.
- williamdclt 9y agoWhat I like in redux is not redux, it's everything built on it. Redux-form, redux-saga, react-redux-router, redux-persist...
- k__ 9y agoSure thing. That's what I hear from every Redux proponent. And I guess if you put in the time and became a Redux pro, there is no reason to switch. But if you're starting out or have to work with non-pros, you're probably better served with unstated.
- williamdclt 9y agoI'd have trouble seeing how I would do as good with Unstated as with Redux, but I have lot of experience with the later and zero with the former so you might very well be right!
- k__ 9y agoRedux is more powerful, but the question is simply, do you always need that power? If you're a single dev who knows Redux or working in a project with Redux pros, I'd say go for it, you invested your time into it, why not use it.
- bromuro 9y agoI think the opposite, redux-form for example is an over engineered way to handle forms. And not let me talk about react-router-redux. This is actually my problem with redux - developers tend to put everything in the store.
- mettamage 9y agoIt seems interesting. I have one code reading issue, could anyone be so kind to explain it? I read things like: <Subscribe to={[CounterContainer]}> Why are [ ] needed? And what do [ ] mean here? I thought { } were enough for evaluating code.
- devrelm 9y agoIt looks like Subscribe's `to` prop expects an array. So, `[CounterContainer]` is just an array with a CounterContainer in it. The `{ }` that wrap it are just part of the syntax of passing props. If you wanted to pass an object literal to a prop, you'd have to double-up the curly braces: <div style={{ color: 'white' }} /> And strings can be passed with or without curly braces: // both of these are fine: <div id='foo' /> <div id={'foo'} />
- xatxat 9y agoThey are used to create arrays. You are able to subscribe to multiple containers: <Subscribe to={[CounterContainer, SomeOtherContainer]}> {(counter, someOther) => ()} </Subscribe>
- thaunatos 9y agoI believe that the [] indicate an array.
- mettamage 9y agoAh, right! It just evaluates to JS, fair enough. Thank you all!
- eyelidlessness 9y agoAlmost. It evaluates JS expressions (not statements). It's a minor distinction but trips a lot of people up.
- akmittal 9y agoGenuine question: why not have a singleton service and store/get data from there(Just like angular services)
- cheez 9y agoThe idea is to have a single immutable data store. Immutability gives you, among other things, easier time debugging and reasoning. Having a single, global data store means that the entire state of your application can be managed centrally. There are no seemingly unimportant variables hiding anywhere that would drive your app, it's all in one place.
- xatxat 9y agoYou can do that, but you will need to find a way to re-render the components which use that service, after something changed.
- k__ 9y agoI think this works well for Angular, because it's based on observables. Rendering is just a function from state->ui. If they build their state-container on observables too, the re-render will follow implicitly by subscribing to it.
- ojosilva 9y agoI think that's what mobx is all about. I create a simple Javascript class called WhateverStore and make it observable. Then all the UI components are observer of an instantiated WhateverStore and that's about it.
- remost 9y agoJust do it the React way and re-render everything by calling ReactDOM.render() again. In most cases that is fast enough.
- k__ 9y agoRendering in React is based on state and props. If you want to trigger a render, you need to change one of these. If you base it on some external entity, you need to trigger a render manually. These state libraries usually let you wrap your component in another component, that passes the state down as props, so when the state changes, new props are passed down and your component automatically renders with the new state.
- lucidone 9y agoAs a contrary opinion, I find Redux simplifies my react projects by virtue of having a standardized set of practices for handling state. The flux pattern in general is amazing, and I always wrote spaghetti, passing props everywhere, prior to discovering it.
- k__ 9y agoAfter I switched to a 3 layer architecture (1. root component, 2. screen components, 3. ui components) I just had to gather data asynchronously in the screens and pass it into the UIs via props and that was it. No spaghetti, no Actions/Reducers. I'm not saying this works for all apps, but I got pretty far with it.
- lolive 9y ago3-tier on the client :)
- vanderZwan 9y agoDo you have an example somewhere on-line? I'm also not familiar with the term "screen components"
- k__ 9y agoOn mobile apps, the "pages" are called "screens" it's just the main UI container that gets replaced if you switch between screens
- vanderZwan 9y agoAh, that makes sense. Thanks!
- deleted 9y ago[deleted]
- swoongoonz 9y agoWhat I like about Redux is the conventions in place to update the store from a number of places. I currently work on a complicated CRM with state needing to change from a number of places. Having a convention and having the ability to update state and manage side effects with sagas has been game changing. If you're making a todo app, yeah sure, no redux. Creating an app without conventions to manage state? good luck making all of those decisions.
- hitekker 9y agoAgreed. Half the problem with Redux is technical, the other half is political. Up until recently, the maintainers of Redux have neglected to meaningfully rein in the hype. If your react app is "complex" and "depending on your usecase"[1], redux must be used. Which is why the very first tutorial in its documentation is a... TODO app. A basic concept deliberately over-engineered to fit a framework that isn't suppose to handle "simple" apps. What? Developer under great social pressure must resolve this glaring dissonance themselves. Developers who may feel more insecure in their knowledge of front-end engineering than they would like to admit. [2] Well, for these developers, it can't be that they're not be "smart" enough. On the contrary, with some mental gymnastics [3], this "smart" framework will solve their "smart" problems, and then they will be "smart". The unspoken corollary is, of course, that anyone who disagrees with the "smart" framework" is not "smart". What begins as misleading by the maintainers creates poison in the enterprise. Suddenly, this popular framework becomes the norm, as team after team is forced to conform, applying this "smart" framework to domains that aren't explicitly forbidden by the docs, to consistently, poor results. Thus the driving force behind Redux became the deep, deep denial of insecure engineers, imprisoned by their own monoliths. Finally, there's a backlash. Without strong-technical-merits or clear-headed communication by the contributors, Redux will likely deflate and be limited to legacy code and the few cases it supports tremendously well. Essentially, for those who've been around the block, Redux is to React what AngularJS 1.0 was to JS frameworks. A shame that we had to suffer through this cycle again. [1] Dev-speak for "If you know what's good for you" [2] As said recently, "Most Developers would not hire themselves" [3] The test for this is if the Redux TODO app makes sense to them.
- danabramov 9y ago"Up until recently" I wrote this a year and a half ago: https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367 https://medium.com/@dan_abramov/you-might-not-need-redux-be4...
- hitekker 9y agoI have respect for your work and your writing. But neither your blog nor the OP's is the source-of-truth. It's the documentation which is why I qualified my statement with "meaningfully". Controlling the hype bubble would have meant rewriting the introduction of the docs, for example, replacing the TODO app with something actually intelligible. This drastic act would also have required buy-in from all the maintainers-- one of whom just posted an article protesting too much the decline of the framework in question. From an outside's perspective, the damage control seems too little, too late.