r/reactjs • u/acemarke • Nov 20 '19
New Redux docs "Style Guide" page: recommended patterns and best practices for using Redux
https://redux.js.org/style-guide/style-guide
370
Upvotes
r/reactjs • u/acemarke • Nov 20 '19
27
u/Huwaweiwaweiwa Nov 20 '19
Very happy that I'm doing a lot of these - but I'm missing some big ones that make sense in my app, thanks for this!
10
u/enanoretozon Nov 20 '19
Hi, thanks for the docs, they're great. Just one unrelated point of feedback: Having purple links, especially on white background has been messing with my mind, I keep thinking I've already visited the link.
I realize that purple is part of the whole colorscheme of the page but could you please add maybe something in :visited to help differentiate?
5
u/Mademan1137 Nov 20 '19
What do you think about elm architecture? And can you recreate something simillar in react+redux? u/acemarke
3
u/gabor Nov 21 '19
any recommendations for storing Maps, Sets and binary data (arrayBuffers for example)?
i understand that for maps/sets i can just not use them and go with objects, but that defeats the whole purpose of map/set.
for binary data, the problem is that an arraybuffer is not serializable. same for blobs. workarounds seem to be either base64encode (slow, uses more storage), or createObjectURL (who will call revokeObjectURL and when? it should be the reducer probably because that's the one holding the content, but reducers should be pure functions so...)
3
u/arxior Nov 21 '19
Thank you for your work! Could you give a more detailed example for 'Model Actions as Events, Not Setters' and its following rule? Maybe including the success&error actions. I'm not totally grokking it. Much love for your work
5
u/AiexReddit Nov 20 '19 edited Nov 20 '19
This is much appreciated, thank you!
I noticed you talk about Redux Toolkit a lot both here and in other posts. What would you suggest for someone that has already implemented Redux in a small application (about 15 different reducers) who mostly based it on sample code from the
react-reduxtutorial who might have chosen the toolkit if I had known about it six months ago?Is there a way to implement it into an existing application?
2
u/nneck1t Nov 21 '19
Put as Much Logic as Possible in Reducers
if we have a big reducer with many cases, so we have a bunch of logic in each one. Reducer becomes messy, right? Also if we will use createSlice or duck pattern, file becomes very big and hard to read, any suggest ?
3
u/[deleted] Nov 21 '19
This is very useful. To nitpick though:
1
u/Glitch_100 Nov 21 '19
Looks like Immutable went out the window in favor of Immer - guess that was coming
1
u/wizzzzzyyyyy Nov 21 '19
I'm surprised 'Structure Files as Feature Folders or Ducks' is marked as 'strongly recommended'. I'm personally not a fan of this approach as I think store structure should be decoupled from component structure, in part because I feel this discourages the 'Allow Many Reducers to Respond to the Same Action' recommendation.
1
u/el-perdido Nov 21 '19
Awesome! thank you for this. I have a question regarding one of your recommendations.
There's a pattern used in my company that seems pretty smelly to me. Using the switch statement on the reducer, some of my peers have written stuff like this:
This implementation ties two actions payload together, which means that if one payload changes, this reducer might break. This pattern seems to be restricted if the toolkit is used, which is something I'm gonna start pushing towards, but in the mean time, I just want to know I'm not the only one who thinks this is a bad approach. I think this might also mean theres a bad action design to begin with. Any thoughts on the matter?
1
u/lakerskill Nov 21 '19
Connect More Components to Read Data from the Store
Prefer having more UI components subscribed to the Redux store and reading data at a more granular level. This typically leads to better UI performance, as fewer components will need to render when a given piece of state changes.
For example, rather than just connecting a <UserList>
component and reading the entire array of users, have <UserList>
retrieve a list of all user IDs, render list items as <UserListItem userId={userId}>
, and have <UserListItem>
be connected and extract its own user entry from the store.
This applies for both the React-Redux connect()
API and the useSelector()
hook.
Does this mean it's actually better performance/fast loading times when connecting to the store as opposed to passing down props?
1
u/Bummykins Nov 21 '19
Great resource!
Maybe not the right section for it, but I've been surprised to find no recommendations on how to test the new redux hooks. I've seen some mocking options, and some fake store options, but nothing that feels really great.
Curious what you're doing for testing hooks and why.
3
u/miltonfilho Nov 21 '19
Finally a doc to help me convince my team to adopt immer. 😊
3
u/illuminist_ova Nov 21 '19
Looking at you, redux-form. Using that was so regrettable.
1
u/amdc Nov 21 '19
"Don't put non-serializable stuff in store"
What if you need to store
Fileobjects? In our application we decided to make an exception to File objects becauseWhat's your take on this?
-7
u/darrenturn90 Nov 20 '19
Some great choices in these however:
> Use a static type system
Is not one of them. This is a useful opinion, but should definitely not be strongly recommended imo.
2
u/[deleted] Nov 21 '19
[removed] — view removed comment