← All videos

Engineering video

22:01 · 3 October 2024

how Senior Devs think about React & some code refactoring

Chapters 4 sections

  1. 0:00Introduction and Functional Requirements
  2. 9:18Code Organization and SOLID Principles
  3. 12:00Non-Functional Requirements: Accessibility, Security, and Performance
  4. 17:00Advanced Topics and Conclusion

Key concepts in this video:

1. Functional Requirements and States

When fetching data from a backend, applications must handle loading and error states in addition to the data itself to provide a stable and predictable user experience.

Watch section from 0:00 →

2. Separation of Concerns via SOLID

Applying the Single Responsibility Principle by splitting the dropdown into a logic wrapper and a stateless presenter component improves readability, maintainability, and testability.

Watch section from 9:55 →

3. Accessibility and Semantic HTML

Using native semantic tags ensures the DOM is accessible out of the box, helping avoid the anti-pattern of maintaining excessive ARIA attributes on non-semantic wrappers.

Watch section from 14:00 →

4. Web Security and Sanitization

Rendering content fetched from servers requires considering sanitization to prevent potential XSS attacks if user-persisted data or unverified inputs are ever rendered.

Watch section from 14:48 →

5. React Performance Optimization

Using React.memo on the presenter component ensures it only re-renders when its props actually change, optimizing browser performance.

Watch section from 15:56 →

Transcript

Hi there folks. So let's jump right in. What we will do today is to look at a component that somebody else, in this case it's me, and this component has what I call junior level code in React and we will factor this to get it all the way to senior level. And along the way we will see how a senior thinks and what are the guiding principle a senior developer would use to kind of solve this problem. So you can incorporate this in your code. Let's get going. Um, let's first overlook what we are building. So basically what we're building, it's a drop down component. It has some options and a selected value. The options get fetch over the network. So basically, we get those options using a function that mimics a network behavior and then we basically render them and we render an option for each one of them. So, this is a basic select element and whenever the user clicks on it, we actually update this state, which is the selected value. Let's see how this looks like. Let me run this. If I'm not running it already, I might have an open tab. Perfect, it's already running. And basically, this is how it looks like. Right now, the component is loading. And I think yes, perfect. And these are the options that came through. Whenever I select one of those, it gets selected. That's pretty much it. Now, this is a junior level implementation. It does satisfy most of our functional requirements, but not all of them. And we will start there. So, always keep this in mind. Whenever you don't know how to start in interviews and so on, we always start with the functional, functional requirements. Which is basically, hey, what do I need in order for this to work? What does the user needs to interact with? So, functional requirements. Now, on the surface, it looks like this, the drop down satisfies all of those. But in reality, there is a big flaw here, which is whenever we synchronize state with the backend, or as civilians like to call it, fetching data. Whenever we get something from the backend, like in this case, we get the options, we have two extra states that are not the data. And those states, as you're probably thinking, are loading and error. And this is always the same. So keep this in mind, if you fetch data, you need the loading and error state. Most libraries these days like React Query, give