News for hackers. Stuff that matters.
Re:Redux – Not Dead Yet (2018) by warp on Thursday April 09, 2020 @10:16AM (#22822734)
I've recently tried using Redux for a side-project. While it solves the problem (organizing state), I feel there is too much boilerplate for my small project.

Are there any popular alternatives which don't use reducers?

EDIT: thanks for all the responses, I'll check them out :)

Re:Redux – Not Dead Yet (2018) by FlorianRappl on Thursday April 09, 2020 @10:27AM (#22822825)
There are some which require less boilerplate and actually are (partially significantly) smaller in size.

https://github.com/jamiebuilds/unstated https://github.com/dai-shi/react-tracked https://github.com/react-spring/zustand https://github.com/diegohaz/constate

(there are more, but these may be among the most popular)

You can find a comparison (and more) here: https://github.com/dai-shi/lets-compare-global-state-with-re...

Re:Redux – Not Dead Yet (2018) by rareba on Thursday April 09, 2020 @10:36AM (#22822921)
Redux Toolkit https://github.com/reduxjs/redux-toolkit solved the problem of having too much boilerplate for me. Slices also make it organizing state a breeze.
Re:Redux – Not Dead Yet (2018) by tarcon on Thursday April 09, 2020 @10:23AM (#22822789)
You can use a React Context and have simple global state in your React app. If you like reducers, throw in a useReducer hook and you have a large part of what people use Redux for.
Re:Redux – Not Dead Yet (2018) by bufferoverflow on Thursday April 09, 2020 @07:22PM (#22828272)
I've done that recently, and I had many performance issues. I have a long-ish list of very simple components that are in a sorted order. So when I change the value of one, I re-sort the list. I was hoping React would only rerender the changed items (remove old, insert new). But no, it rerenders the whole list, which takes 500ms. I made sure object references in the list stay the same, that didn't help.

I can implement this thing in vanilla JS, and it would not take more than a few milliseconds. React is cool, but so damn hard to optimize.

Re:Redux – Not Dead Yet (2018) by acemarke on Thursday April 09, 2020 @08:07PM (#22828569)
It's important to distinguish between React "rendering" (asking components what they want the UI to look like), and actually applying updates to the DOM.

React's default behavior is that when a component renders, React will recursively re-render _all_ of its descendants. it then diffs the VDOM render output to see if you've asked for any changes to the UI since the last time it updated. If the two VDOM trees are the same, React says "nope, nothing to do here!", and ends the render pass. _If_ you requested changes, it then gathers them up and applies them all to the DOM in one step.

You can optimize the rendering behavior in some ways if needed. The `React.memo()`, `PureComponent`, and `shouldComponentUpdate` APIs [0] allow you to tell React to skip re-rendering if you know that a component's props haven't meaningfully changed, and thus would result in the same UI output as the last time. This does require that the child components be separate component types (ie, `<MyComponent />`), not just the parent rendering a bunch of plain elements (like `<div>`).

For lists in particular, React needs to know how to identify which items are which. React doesn't care about object references, but you can pass a special prop called a `key` when rendering items in a list. That way, if you move a list item up, swap a couple others, delete one, and add a couple more, React knows what existing list items ended up where, and can update the DOM efficiently in response. Otherwise, changes to lists without good use of keys can result in more DOM work being done than actually necessary.

[0] https://reactjs.org/docs/react-api.html#reactmemo

[1] https://reactjs.org/docs/lists-and-keys.html

Re:Redux – Not Dead Yet (2018) by dointheatl on Thursday April 09, 2020 @11:09AM (#22823264)
I've recently had success using https://github.com/rematch/rematch for a small project. Significantly less boilerplate, though admittedly the documentation is a bit sparse.
Re:Redux – Not Dead Yet (2018) by SaltyBackendGuy on Thursday April 09, 2020 @10:34AM (#22822908)
I had a similar problem when I started using Redux for a side project. If it's a simple enough app (as mine was) try https://www.npmjs.com/package/unstated. I am not a React expert but this was super simple and easy to get up and running with.

[No affiliation with the project]

Re:Redux – Not Dead Yet (2018) by eropple on Thursday April 09, 2020 @10:21AM (#22822769)
There's stuff like MobX, but much of the point of Redux is the reducer step.

You may be happy with just using hooks, though.

Re:Redux – Not Dead Yet (2018) by ccmcarey on Thursday April 09, 2020 @10:27AM (#22822830)
Do you mean the point of Redux?
Re:Redux – Not Dead Yet (2018) by eropple on Thursday April 09, 2020 @10:42AM (#22822975)
You saw nothing. Nothing!

(Yes, thank you, edited.)

Re:Redux – Not Dead Yet (2018) by sergiotapia on Thursday April 09, 2020 @11:00AM (#22823169)
Use mobx! It's 5% of the code you would have to write using redux and their gigantic box of tools.
Re:Redux – Not Dead Yet (2018) by reflectiv on Thursday April 09, 2020 @11:10AM (#22823270)
Also check out mobx-state-tree...made by the same people and more suited towards large applications.

I personally love mobx-state-tree (and mobx!). I really wish more would not jump straight for redux/etc without giving the powerful abstraction of observables via mobx a shot.

Re:Redux – Not Dead Yet (2018) by doomguyfromhell on Thursday April 09, 2020 @10:59PM (#22829566)
Yes, mobx state tree is great and should be use more frequently.
Re:Redux – Not Dead Yet (2018) by gunn on Thursday April 09, 2020 @11:23AM (#22823417)
Try mine! https://github.com/gunn/pure-store

Super simple, 100% test coverage, 100% typesafe if using typescript, absolutely no boilerplate.

Re:Redux – Not Dead Yet (2018) by runawaybottle on Tuesday April 14, 2020 @01:11AM (#22862935)
Edit: Just checked out the code. This really looks like an event emitter with Immer. Is that fair to say? I totally dig the simplicity of it.
Re:Redux – Not Dead Yet (2018) by thekemkid on Thursday April 09, 2020 @10:21AM (#22822766)
hooks and the context api are great for small projects. Check out the hooks comparison section in the linked article to understand how it might fit your app. It references other good resources, too.
Re:Redux – Not Dead Yet (2018) by l1ambda on Thursday April 09, 2020 @10:30AM (#22822865)
The Reducks pattern https://github.com/erikras/ducks-modular-redux fixes the boilterplate problem by keeping your action types, action creators, and reducers in the same file.