2021-11-25
2386
#react
Chak Shun Yu
78806
Nov 25, 2021 â‹… 8 min read

How to write more readable React code

Chak Shun Yu A software engineer with a current focus on frontend and React, located in the Netherlands.

Recent posts:

Radix Ui Adoption Guide Overview Examples And Alternatives

Radix UI adoption guide: Overview, examples, and alternatives

Radix UI is quickly rising in popularity and has become an excellent go-to solution for building modern design systems and websites.

Nefe Emadamerho-Atori
Apr 25, 2024 â‹… 11 min read
Understanding The Css Revert Layer Keyword, Part Of Css Cascade Layers

Understanding the CSS revert-layer keyword

In this article, we’ll explore CSS cascade layers — and, specifically, the revert-layer keyword — to help you refine your styling strategy.

Chimezie Innocent
Apr 24, 2024 â‹… 6 min read
Exploring Nushell, A Rust Powered, Cross Platform Shell

Exploring Nushell, a Rust-powered, cross-platform shell

Nushell is a modern, performant, extensible shell built with Rust. Explore its pros, cons, and how to install and get started with it.

Oduah Chigozie
Apr 23, 2024 â‹… 6 min read
Exploring Zed, A Newly Open Source Code Editor Written In Rust

Exploring Zed, an open source code editor written in Rust

The Zed code editor sets itself apart with its lightning-fast performance and cutting-edge collaborative features.

Nefe Emadamerho-Atori
Apr 22, 2024 â‹… 7 min read
View all posts

One Reply to "How to write more readable React code"

  1. Not sure I agree with a for loop over reduce.

    First I don’t think the list for things you have to keep in mind is correct. Why are you thinking about the previous value? If you’re doing that then you’re thinking about things being combined in a linear fashion which isn’t great. You should be thinking with pure functions when using functional methods.

    Also with the order – you already have the lengths and the limits. It’s the length of the array. It’s baked in. You get that for free. So the only thing with order is declaring the initial state. That’s also easy, just declare the variable first like you would with a for loop and pass it in if that’s the issue. But there is also a bonus with reduce – it takes a function. That means you can move that function out and just use a name. `ids.reduce(addItemToObject, {})`.

    The issue with a for loop is it can do anything. With reduce you are already hinting at what you want to do (i.e. reduce an array). The other factor that comes in is that with reduce you are expected to use a pure function (if you’re not then that’s a whole other issue). That takes a lot of context out of the equation, whereas a for loop can be using context from anywhere. So the only thing that is weird about it is the order.

Leave a Reply