Senior Frontend Developer Interview Questions 2024

theSeniorDev
27 Mar 202425:52

Summary

TLDRIn this video, Senior Dev and bden explore advanced front-end topics, focusing on webpack, CSS-in-JS, React, and web performance. They discuss webpack's role in module bundling and tree shaking, the dynamic capabilities of CSS-in-JS, and React's component patterns including error boundaries and hooks. The conversation delves into state management strategies, the balance between essential and derived state, and the implications of using React context for state. Testing approaches for React applications are examined, emphasizing the importance of a mix between unit, integration, and end-to-end tests. The discussion concludes with web performance optimization, particularly focusing on improving First Contentful Paint (FCP) and considering server-side rendering for faster load times.

Takeaways

  • 🔧 Webpack is a module bundler primarily used to combine multiple JavaScript files into a single bundle for efficient loading.
  • 🌳 Tree shaking is a technique used by Webpack to remove unused code from the final bundle, optimizing the application size.
  • 🚀 Webpack's tree shaking requires ES6 static imports and won't work with CommonJS dynamic imports, which are evaluated at runtime.
  • 🌐 Dependency graph is a structure built by Webpack starting from the entry point, tracing all imports to form a tree of modules.
  • 💄 CSS-in-JS allows for dynamic styling by writing CSS inside JavaScript files, enabling style changes based on component state or props.
  • 🚨 CSS-in-JS can lead to larger bundles, difficulty in caching styles, and challenges in debugging due to generated class names with unique hashes.
  • ⚖️ React's PureComponent helps in preventing unnecessary re-renders by shallowly comparing props and state, but is less necessary with Hooks.
  • 🛡️ Error boundaries in React catch JavaScript errors in child components, prevent the error from propagating, and allow UI recovery.
  • ⚙️ useEffect Hook in React is used for handling side effects, such as API calls or subscriptions, in function components.
  • 🔄 React's Context API is suitable for managing global state like authentication and user settings that affect the entire application.
  • 📊 Code coverage is important but should be balanced; aiming for 60-70% in front-end applications can be practical while avoiding the pitfalls of excessive testing for the sake of coverage.

Q & A

  • What is Webpack and what is it used for?

    -Webpack is a module bundler primarily used for bundling JavaScript files into a single file. It was necessary because browsers required a script tag for each JavaScript file, which was impractical for large applications with many files.

  • What is tree shaking in the context of Webpack?

    -Tree shaking in Webpack refers to the process of eliminating unused code from the final bundle during production. It identifies and removes modules or parts of modules that are imported but not actually used in the code.

  • How does tree shaking work with ES6 static imports in Webpack?

    -Tree shaking works with ES6 static imports because Webpack can understand and track these imports to build a dependency tree, allowing it to determine which parts of the code are not used and can be removed.

  • Why can't CommonJS modules with 'require' be tree shaken by Webpack?

    -CommonJS modules with 'require' cannot be tree shaken because they are dynamic and evaluated at runtime. Webpack cannot use these to build a dependency tree to determine which code to drop, as it cannot predict what will be executed.

  • What is a dependency graph in Webpack and how is it used?

    -A dependency graph in Webpack is a structure that Webpack builds from the entry point of the application. It traverses all import statements to form a tree structure that represents all modules. This graph is used to optimize the bundle by determining which modules to include or exclude.

  • What is CSS-in-JS and why is it used?

    -CSS-in-JS is a technique where CSS is written inside JavaScript files. It allows for dynamic styling based on JavaScript variables and state, enabling styles to change in response to user interactions or other runtime events.

  • What are some disadvantages of using CSS-in-JS?

    -Disadvantages of CSS-in-JS include the inability to perform certain CSS optimizations like caching, making the CSS non-cacheable and potentially increasing load times. It can also complicate debugging due to unique hash class names generated by tools like Webpack and may lead to performance issues like cumulative layout shift.

  • What is the difference between a Pure Component and a Functional Component in React?

    -A Pure Component in React is a class component that avoids unnecessary re-renders by comparing incoming props with existing props. A Functional Component is a component written as a JavaScript function, which can use Hooks to manage state and side effects without the need for a class-based approach.

  • What is an Error Boundary in React and why is it used?

    -An Error Boundary in React is a component that catches JavaScript errors anywhere in its child component tree, logs those errors, and displays a fallback UI instead of the component tree that crashed. It helps in isolating errors and preventing the entire application from crashing.

  • Can you explain the useEffect Hook in React and its purpose?

    -The useEffect Hook in React is used to perform side effects in function components. It allows you to perform operations such as data fetching, subscribing to subscriptions, or manually changing the DOM in response to state or prop changes.

  • Why shouldn't you use an async function as a callback to useEffect?

    -You shouldn't use an async function as a callback to useEffect because useEffect expects a cleanup function to be returned, which allows for cleaning up side effects. An async function implicitly returns a promise, which React cannot use as a cleanup function, leading to potential memory leaks or unintended behavior.

Outlines

00:00

🛠️ webpack and Frontend Tooling

The paragraph discusses the role of webpack as a module bundler in frontend development. It explains how webpack consolidates multiple JavaScript files into a single bundle to optimize browser performance. The conversation covers the concept of 'tree shaking' in webpack, which eliminates unused code from the final bundle for better performance. The speakers also touch on the importance of using ES6 static imports for effective tree shaking and the challenges with CommonJS imports. Additionally, the concept of a dependency graph in webpack is explained, which is crucial for module bundling and optimization.

05:01

💅 CSS-in-JS and Its Impact

This section delves into CSS-in-JS, a technique that allows embedding CSS directly within JavaScript files. The speakers explore the benefits of CSS-in-JS, such as dynamic styling based on component states and variables. However, they also discuss the downsides, including the reduced ability to optimize and cache CSS, making it non-cacheable and potentially leading to larger bundles. The debugging complexity due to webpack-generated class names with unique hashes is also mentioned, along with performance implications like cumulative layout shift and the creation of a deep component tree.

10:01

🔄 React Components and State Management

The discussion shifts to React, starting with the concept of a 'Pure Component' in React, which helps prevent unnecessary re-renders by comparing props. The conversation then moves to 'Error Boundaries,' components designed to catch and handle errors in child components, preventing the entire application from breaking. The use of React Hooks, particularly 'useEffect,' is explained, highlighting its role in managing side effects. The difference between essential and derived state is also explored, emphasizing the importance of minimizing state for better performance and maintainability.

15:02

📊 State Management Strategies in React

The paragraph focuses on strategies for managing state in React applications, especially when dealing with backend data, authentication, and user settings. The speaker suggests different approaches based on the nature of the state: using local component state for data that's not widely used, React Context for globally accessible authentication state, and potentially Redux or useReducer for complex state transitions within user settings. The trade-offs of using React Context, such as increased re-renders, are also discussed.

20:02

🔍 Implementing Testing in React Applications

This section addresses how to implement testing in a mid-sized React application that previously had no tests. The speaker advocates for a balanced approach, focusing on end-to-end (E2E) tests to cover features, unit tests for reusable components, and strategic integration tests. The importance of code coverage is discussed, with a recommendation to aim for 60-70% in frontend applications to ensure test reliability without overdoing it. The speaker also shares experiences from different companies, highlighting the challenges of enforcing high code coverage percentages.

25:04

🚀 Web Performance Optimization

The final paragraph discusses web performance, specifically focusing on the First Contentful Paint (FCP) metric. The speaker identifies common causes of a bad FCP score, such as large client-side JavaScript bundles, lack of CDN usage, and excessive CSS in the header. Strategies for improving FCP include setting up a CDN, code splitting, and potentially using server-side rendering for very large applications where client-side rendering is too slow. The speaker also notes that server-side rendering adds complexity and should be considered based on the application's needs, such as SEO sensitivity.

Mindmap

Keywords

💡Webpack

Webpack is a module bundler primarily used for JavaScript applications. It packages various files into a single bundle, which is crucial for optimizing load times in web browsers. In the script, Webpack is discussed as a tool for enhancing frontend development efficiency by reducing the need for multiple script tags and enabling code optimization techniques like tree shaking.

💡Tree Shaking

Tree shaking is an optimization technique used in Webpack to remove unused code from the final bundle. It helps in reducing the size of the JavaScript files sent to the client, thereby improving performance. The script mentions tree shaking as a feature available in Webpack 5 and its dependency on static ES6 imports to function effectively.

💡Dependency Graph

In the context of Webpack, a dependency graph is a data structure that represents all the dependencies within a project. Webpack uses this graph to determine which modules to include in the final bundle. The script explains how Webpack builds this graph starting from the entry point and traversing through all import statements to optimize the bundle for production.

💡CSS-in-JS

CSS-in-JS refers to the practice of writing CSS styles directly within JavaScript files. This approach allows for dynamic styling based on component state and props. The script discusses CSS-in-JS as a solution for interactive styling needs in modern web applications, highlighting its advantages in creating dynamic styles and mentioning the challenges it poses in terms of caching and debugging.

💡React

React is a popular JavaScript library for building user interfaces, especially single-page applications. It is mentioned throughout the script as the framework of choice for the discussed projects. Topics such as React components, hooks, and error boundaries are explored in relation to React, emphasizing its role in modern frontend development.

💡Pure Component

A pure component in React is a type of component that prevents unnecessary re-renders by shallowly comparing its props and state. The script explains that with the introduction of hooks in React, the need for pure components has diminished since hooks inherently provide similar performance optimizations.

💡Error Boundary

Error boundaries in React are special components used to catch JavaScript errors anywhere in their child component tree and handle them gracefully. The script discusses their utility in preventing a single component's error from crashing the entire application, thus improving the application's robustness and user experience.

💡useEffect Hook

The useEffect hook in React is used to perform side effects in function components. It allows for operations such as data fetching, subscriptions, or manually changing the DOM. The script touches on the proper use of useEffect, its impact on performance if misused, and the importance of returning a cleanup function to prevent memory leaks.

💡State Management

State management in the context of the script refers to the strategies used to handle and distribute state within a React application. It includes discussions on local component state, global state using React Context, and complex state logic potentially requiring external libraries like Redux. The script emphasizes the importance of choosing the right state management approach based on the application's needs.

💡Code Coverage

Code coverage is a metric that measures how much of an application's code is executed during testing. The script discusses the balance between high code coverage and practical testing strategies, suggesting that while high coverage is desirable, it should not lead to writing tests that do not add value, and that maintaining a coverage level of 60-70% is a good target for frontend applications.

💡First Contentful Paint (FCP)

First Contentful Paint (FCP) is a web performance metric that measures the time it takes for the first piece of content to be rendered on the screen after a user navigates to a webpage. The script identifies FCP as an important metric for user experience and discusses various optimization strategies to improve FCP, such as reducing JavaScript bundle size, using a CDN, and implementing code splitting.

Highlights

Introduction to senior frontend interview questions by D and bden.

Explanation of webpack as a module bundler and its necessity.

Discussion on webpack's role in optimizing JavaScript file handling for browsers.

Experience with webpack configurations and project setups shared.

Tree shaking in webpack to eliminate unused code for optimized bundles.

The importance of using ES6 static imports for effective tree shaking.

Challenges with tree shaking and dynamic imports using CommonJS.

Understanding webpack's dependency graph for module management.

CSS-in-JS explained as a solution for dynamic styling with JavaScript.

Advantages of CSS-in-JS for immediate style changes based on state.

Disadvantages of CSS-in-JS including caching issues and increased bundle size.

Impact of CSS-in-JS on debugging and performance due to component re-renders.

React's Pure Components and their role in preventing unnecessary re-renders.

Error boundaries in React for localizing and managing errors in components.

Use of React Hooks, specifically useEffect, for handling side effects.

Why async functions are not suitable as callbacks for useEffect.

Strategies for state management in React applications with various requirements.

Difference between essential and derived state in React components.

Disadvantages of placing state in React Context, such as increased re-renders.

Approach to testing a mid-sized React application with no existing tests.

Importance of balancing unit, integration, and end-to-end tests in a React application.

Code coverage as a metric for testing effectiveness and its appropriate levels.

Real-world experiences with code coverage expectations in different industries.

Explanation of FCP (First Contentful Paint) and its significance in web performance.

Causes of a bad FCP score and strategies for improvement.

Considerations for server-side rendering to enhance FCP in large React applications.

Conclusion of the discussion and预告 of future content.

Transcripts

play00:00

hi folks D here from the senior Dev and

play00:02

uh today together with bden we're going

play00:04

to go to a bunch of senior frontend

play00:07

interview questions these are real

play00:09

interview questions that we've got

play00:11

ourselves in senior front interviews but

play00:14

also our mentees number one we're going

play00:16

to start talking about tooling right and

play00:18

the first question is can you tell me

play00:20

about your experience using webpack what

play00:23

is it use for uh weback it's a module

play00:26

bundler so we basically use it to uh put

play00:29

several JavaScript files into a single

play00:31

one that used to be necessary because in

play00:34

the browser um you basically in order to

play00:36

add JavaScript you need to place a

play00:38

script tag for every Javascript file you

play00:40

have and when you have a big application

play00:41

with hundreds of files that's just not

play00:43

practical so bundle um weback will uh

play00:46

will kind of bundle all that it will

play00:47

parse all of them and based on your

play00:49

imports and exports and put them all

play00:51

together into a single bundle and then

play00:52

we can apply different optimizations um

play00:54

I worked a lot with weback in the past

play00:57

basically setting up projects from

play00:58

scratch and doing all kinds of

play01:00

configurations yeah nice Bon are you

play01:02

familiar talking about weback and module

play01:04

bunders are you familiar with the term

play01:06

tree shaking three shaking as in weback

play01:10

it's basically what weback will do when

play01:12

we ship our bundle to production and it

play01:14

will basically try to find that code

play01:16

those are modules that maybe we imported

play01:18

in our code but we're not really using

play01:20

or parts of modules and it will

play01:22

basically try to eliminate them so our

play01:24

bundle is as small as possible and we

play01:26

get the best web

play01:28

performance amazing have you used um

play01:30

tree shaking professionally in

play01:32

production uh at any stage can you uh

play01:34

give me an example of

play01:36

that uh yes as far as I know in version

play01:41

five it comes by default so every time

play01:42

we use weback it will try to T Shake our

play01:44

code one thing you want to be aware of

play01:47

is that it only works with es6 static

play01:49

import so if you have any any module in

play01:52

your dependencies or you're using

play01:54

commonjs Imports with require then weac

play01:57

cannot really appre shake that so you

play01:59

got to be careful because yes weback

play02:02

ships with it by default but if we don't

play02:03

use those kind of modules then it's not

play02:06

capable of really understanding if it

play02:08

should remove that code or it's not

play02:09

being used which modules where uh so us

play02:13

six static inputs are the ones that do

play02:16

uh allow weback to do three shaking and

play02:19

commonjs you know the old require

play02:21

notation those ones cannot be three

play02:24

shaked because they are Dynamic and they

play02:25

are evaluated at run time so they they

play02:27

are basically functions and weback

play02:30

cannot really use them to build this

play02:32

dependency tree and figure out okay

play02:33

what's going wrong in there can I drop

play02:35

any of that code until it gets executed

play02:37

in the

play02:38

browser excellent seems like you know

play02:41

quite a bit about weback now um what is

play02:44

talking still talking about weback and

play02:45

and tooling and module bunders um you

play02:48

know what is a dependency graph and what

play02:51

is it used for in weback uh the

play02:54

dependency graph in in weback it's

play02:57

basically what weback builds from our

play02:59

entty point so it'll basically take our

play03:01

first file and then figure out and go

play03:04

down all those import statements we have

play03:06

at the top and from that it will keep

play03:08

going down until it reaches basically

play03:10

the end and that's basically a three

play03:12

structure that it forms and that's how

play03:14

it stores basically all our modules and

play03:17

then it's using that to uh transverse it

play03:20

to go through it and find out if there's

play03:22

any modules we need to drop or which

play03:23

ones are in production so it's the kind

play03:25

of the main abstraction in web pack and

play03:27

it will always be used to put you know

play03:30

to go from Individual files to a single

play03:32

bundle cool now we're GNA move on with a

play03:34

question about CSS injs basically can

play03:38

you explain what CSS injs is and can you

play03:42

give me an example you know what are um

play03:44

what are some use cases for CSS

play03:48

injs uh yeah sure so CSS injs appeared

play03:53

because we writing all this JavaScript

play03:55

and we have a lot we have a lot of

play03:56

interactivity and we basically want um

play03:59

to change CSS Styles when a certain

play04:01

state or certain variable changes and

play04:04

CSS injs allowed us to um to basically

play04:07

have Dynamic CSS where you click a

play04:09

button and some CSS CH changes

play04:11

immediately um you could do that with

play04:13

classes like you would assign a

play04:14

different class to an element but it's

play04:16

just a lot easier if you could just swap

play04:19

colors or directly do things with

play04:20

JavaScript variables inside CSS that

play04:23

usn't that was not possible before

play04:25

because you had static CSS files uh and

play04:28

with CSS injs we write our

play04:30

CSS inside JavaScript files and then

play04:33

webpack will basically somehow extract

play04:36

that and build different classes and

play04:37

attach them to attach them to um to the

play04:41

um to the JavaScript uh to the HTML Dome

play04:44

at run time but we don't need to worry

play04:46

about that so it basically allows you to

play04:47

write your CSS in JavaScript and

play04:49

leverage a lot of dynamic dynamic Styles

play04:52

so you have the dynamic language like

play04:53

JavaScript but you can do styling with

play04:55

it awesome so you already mentioned some

play04:58

of the advantages right of having

play05:00

JavaScript control um variables inside

play05:03

your styling but what are some

play05:05

disadvantages of using CSS

play05:07

injs uh well one of the main

play05:10

disadvantages is that because your

play05:12

styles are now in Javas in JavaScript

play05:14

files you cannot really extract them and

play05:17

do certain optimizations you can do to

play05:19

CSS um and one of the thing is you

play05:21

cannot really cach your CSS files so if

play05:24

your CSS didn't change back in the days

play05:27

you could have the user uh storing that

play05:29

and browser side because you attach a

play05:31

cash policy with HTP headers to it like

play05:34

cash control and they would store that

play05:35

and in a subsequent visit they wouldn't

play05:37

have to download all your CSS but now

play05:39

all that CSS it's inside our uh

play05:42

Javascript file JavaScript bundle it's

play05:44

all together which makes our bundle

play05:45

bigger um it's a bit harder to price but

play05:48

it's also non-cashable uh you can

play05:50

actually counteract that by extracting

play05:53

uh with web pack your CSS at run time um

play05:57

and ship it separately uh but it's still

play05:59

still not as easy as it used to be when

play06:01

you had your CSS separated from from

play06:03

your

play06:04

JavaScript understood it's also harder

play06:06

to debug your CSS uh because again those

play06:09

classes that weback generates they're

play06:11

unique hashes and all of the sudden it's

play06:13

not as easy as it used to be to

play06:15

understand why a certain element looks a

play06:17

certain way there is some tooling right

play06:20

that um csjs usually brings on to make

play06:23

that debugging easier um one thing you

play06:27

we haven't chatted about it's the the

play06:29

performance um how how how does it

play06:32

affect how does CSS njs affects

play06:35

performance so that's uh one of the main

play06:38

things is because you cannot cash your

play06:39

CSS you're loading it and it will

play06:41

decrease um the performance and um yeah

play06:45

it will also you might also have a lot

play06:47

of uh cumulative layout shift because

play06:49

your CSS gets now applied when uh when

play06:53

the JavaScript bundle gets pared so um

play06:57

in the critical rendering path we

play06:58

usually evaluate CSS first and we apply

play07:00

it and that ensures the fact that

play07:02

there's no flash when you load the page

play07:05

but now because we evaluate the CSS at

play07:07

the end you might have cumulative layout

play07:10

shift when the user loads the page

play07:11

things might move around and you got to

play07:13

be careful uh and sometimes split your

play07:15

CSS and ship CSS injs for the dynamic

play07:18

parts and then still use PL old CSS for

play07:20

most of your grid elements so you have a

play07:22

stable page and the other thing is

play07:24

because we are creating new components

play07:26

for example iio style components in

play07:28

react every every time we style a

play07:31

heading or a native react element we end

play07:33

up creating another component and in a

play07:36

big component three that will create a

play07:37

huge component layer uh which it's

play07:40

always a huge component Tre a very deep

play07:42

component Tre which is always a

play07:43

disadvantage because it makes debugging

play07:45

harder and of course a bigger component

play07:47

Tre it's also harder for react to

play07:49

reender all the time even if react is

play07:51

very um effective you want to keep it as

play07:53

small as possible awesome V then we're

play07:54

going to move on to the next question um

play07:56

and we will talk about JavaScript

play07:59

Frameworks specifically react um have

play08:02

you worked with react in production so

play08:05

far uh yes I worked with react for the

play08:08

last uh pretty much since 2015 so that

play08:11

will be around pretty much since it came

play08:13

out yeah awesome then you're probably

play08:15

gonna fly through these questions uh

play08:17

first question is you know what is a

play08:19

pure component in

play08:22

react uh pure component in react well I

play08:26

think so pure component used to be uh

play08:28

when we had components and you wanted to

play08:31

avoid the reenders you use one of those

play08:33

because it would compare the incoming

play08:35

props with the existing props and if

play08:37

they were the same it would skip the

play08:39

render um You probably don't need it

play08:41

today because we use Hooks and hooks are

play08:45

by default memorized so whenever you do

play08:47

a whenever you do a set State um react

play08:50

already checks if you are updating the

play08:51

state to the same value and if that's

play08:53

true it does not reender the component

play08:55

so it kind of comes by default but back

play08:57

with class components we had to use pure

play08:59

compon components cool um another

play09:02

another type of component uh question is

play09:05

what is an error boundary component you

play09:07

know what is it used for why do we have

play09:09

error boundaries components in

play09:12

react uh yeah sure so basically error

play09:17

boundaries um help you limit the impact

play09:20

of an error so if you have a data

play09:21

fetching error in some component you can

play09:23

wrap it with an error boundary and that

play09:26

will uh at some level localize the error

play09:29

so doesn't go up the component three and

play09:30

you don't show the whole thing the whole

play09:32

application broken but you can actually

play09:34

show a placeholder for that one

play09:36

component that um something went wrong

play09:38

uh so it allows you to localize and have

play09:40

a much better predictable UI and also

play09:42

have no layout shift when you have the

play09:45

servers they can totally destroy the UI

play09:47

if they're not managed awesome very well

play09:51

um now still talking about react in this

play09:53

case we will focus on react Hooks and

play09:57

the hook that we are talking about is

play09:59

you know are you familiar with the use

play10:01

effect hook tell me more about how is it

play10:03

used um you know what would be some

play10:05

advantages and disadvantages of using

play10:08

use

play10:10

effect uh yeah sure so we basically use

play10:13

effects to uh trigger a what you call a

play10:16

side effect in react that would mean uh

play10:19

when a state variable changes maybe

play10:21

imagine we want to do something else

play10:22

like we want to make a call to our

play10:24

analytic system and to tell something

play10:26

changed or write into local storage for

play10:29

example example that would be an ideal

play10:30

case for effect um I know when they came

play10:33

out um myself included and all the

play10:35

community kind of abused effects and we

play10:37

using them for everything so we would

play10:39

use effects to change a different when

play10:41

one state variable change we use effects

play10:42

to change a different uh variable but

play10:45

the thing with defs is that they U they

play10:47

run after the component reender and they

play10:49

trigger another reender so if you're not

play10:51

careful and you abuse them you end up

play10:53

having so many reenders in your

play10:55

component and of course reenders of

play10:58

parent uh will cause reender of a child

play11:00

component so that will really it can end

play11:02

up affecting your application if you're

play11:04

abusing them um performance- wise second

play11:08

question about use effect hookit why

play11:10

can't we use an as async function as a

play11:15

callback to use effect well probably

play11:18

because in a sync function returns by

play11:20

default the promise and the return of

play11:23

the use effect hook should always be a

play11:25

cleanup function that allows us to if we

play11:28

for example in the effect attach

play11:29

something to the on scroll effect to

play11:32

remove that Handler because if we don't

play11:34

remove it and we end up re rendering we

play11:36

keep attaching handlers to that event

play11:39

and we might bloat the browser so react

play11:41

allows you to return that cleanup

play11:43

function but when you use a sync you're

play11:45

always returning a promise and react

play11:47

doesn't know what to do with that

play11:48

promise because it's not it's it expects

play11:50

a function so that's why uh you get this

play11:52

lary iting most of the times that tells

play11:54

you hey you cannot you cannot use an an

play11:57

sync function here yeah

play11:59

awesome totally makes sense we are going

play12:02

to move on with a question about State

play12:05

okay and I want you to imagine that we

play12:07

have a simple front end application with

play12:10

the following State okay number one we

play12:12

will be fetching some data from the back

play12:14

end right then we perform some user

play12:17

authentication and um set some general

play12:20

user settings that will affect those

play12:22

those um user settings will affect the

play12:24

whole application and my question is you

play12:28

know when we have to do with such

play12:30

requirements on the client side what

play12:32

would be the best solution to handle

play12:36

State Management in this application and

play12:40

why uh sure okay so we have backend we

play12:44

have data we get from the back end we

play12:46

have authentification state that you

play12:48

mentioned and some global settings right

play12:51

yes um okay so I'll probably Place those

play12:55

differently because of the requirements

play12:56

on them uh so usually uh from my

play13:00

experience data that comes from the back

play13:02

end can stay in component State unless

play13:04

it's needed by two or three components

play13:05

where you could usually just um lift it

play13:08

up and you'll have a bit of prop

play13:10

drilling but it should be manageable

play13:11

with that kind of data um when you have

play13:14

authentification State you usually want

play13:15

that to be globally available to any

play13:19

component because some components might

play13:20

need it to see the user goals and

play13:22

permissions that's why I'll probably use

play13:23

something like react context because

play13:26

it's very useful to broadcast uh the

play13:28

state through the whole component Tre

play13:30

and for the settings um it looks like

play13:34

there might be some non-trivial State

play13:37

transitions in the settings usually if

play13:39

you have maybe complex transitions like

play13:42

a user can be premium or not and that

play13:44

will change many different settings

play13:45

across the apple is that's what I worked

play13:47

with in the past uh in that case you

play13:49

might look at the state machine or you

play13:51

might need a reducer pattern you can of

play13:53

course use something like Redux for that

play13:55

or a smaller like a twoand library but

play13:57

you do need a reducer pattern when you

play13:59

have these complex State transitions um

play14:01

you can use use reducer with context

play14:03

that also works uh so that's how I would

play14:06

um that's how I distribute State these

play14:08

three different ways cool um now still

play14:11

sticking with State um one question

play14:14

about essential and derived State

play14:17

specifically could you explain the

play14:19

difference between essential and derived

play14:23

State uh the difference between

play14:25

essential and the state uh so basically

play14:29

essential state would be state that um

play14:33

changes by itself changes independently

play14:35

and you cannot use it any further and

play14:38

the I state state you could calculate

play14:40

based on the essential state so just to

play14:43

give an example if you had a component

play14:46

that displays like a breakdown of a

play14:49

shopping cart all the different items

play14:51

that you add there they're probably

play14:53

essential state but the total and the

play14:55

vat amount they are the state because

play14:58

you can compute that based on all the

play14:59

other items so that would be an example

play15:01

and usually you want to keep as little

play15:03

State as possible so you want to have

play15:04

only the essential state in your state

play15:07

hooks in react amazing cool um now final

play15:10

question about State and react uh you

play15:14

know what would be the disadvantages of

play15:16

placing State inside react context i'

play15:19

would say number one is that if you lift

play15:21

state so far up anytime that state

play15:24

changes every component that will be

play15:27

connected to that uh context

play15:29

will also change so if you have state

play15:31

that really changes independently and

play15:33

it's consumed by different components

play15:35

you could also split it into different

play15:37

react context to different providers

play15:39

because the components that will

play15:40

subscribe to those will be different

play15:43

you'll have overall less reenders uh so

play15:46

that's one of the biggest problem when

play15:47

you lift state in general but especially

play15:48

when you lift it to context it will end

play15:50

up causing reenders and as a gener rule

play15:53

I try to keep state or we should all try

play15:55

to keep State as close to where it's

play15:57

being used yeah amazing bton um yeah we

play16:01

have a bunch of questions two more two

play16:04

more categories um I will be asking you

play16:06

about uh we're gonna start with testing

play16:08

right and uh specifically you know how

play16:11

would you go about testing a react

play16:14

application yeah what you know if any

play16:17

let's think about a midsized application

play16:19

not too big not too small uh there are

play16:22

no test let's see development team was

play16:23

pretty crazy they've written no test

play16:25

they just you know usually happens we've

play16:28

seen that in companies but they stitch

play16:30

together a bunch of components there's

play16:32

data fetching there's authentication

play16:33

there's all all the kind of

play16:34

functionalities that you find on a

play16:36

client side application built with react

play16:38

how would you go you know you are hired

play16:40

to um bring that up to to implement some

play16:43

best practices in that one specifically

play16:45

testing how would you go about testing

play16:47

this kind of

play16:49

application uh okay so we have a react

play16:51

application that haven't been tested um

play16:54

I would probably inspect the codebase

play16:57

and try to decide either we want to do

play16:59

unit start with unit test or uh

play17:02

integration test or entn test which is

play17:03

the typical testing pyramid um I

play17:06

probably in my experience in the front

play17:08

end one of the best tests to have are

play17:10

entend tests because you test the

play17:12

features which is very clear and then it

play17:13

allows you to refactor your code behind

play17:15

it without changing the test um and if

play17:17

we have any components that we reusing

play17:20

like imagine we might have input fields

play17:22

that are being reused or buttons or any

play17:25

kind of component that has ideally some

play17:26

logic and is being reused like a drop

play17:28

down

play17:29

in that case I would write some unit

play17:31

test on them I'm not a fan of unit

play17:33

testing everything just because in react

play17:36

you didn't get a lot of value for

play17:38

testing pure components like just if an

play17:40

image renders uh we could do that some

play17:42

people do that in order to hit the code

play17:44

coverage uh but I would say having some

play17:46

good ENT test having unit tests on

play17:49

reusable components and strategically

play17:51

choose some integration test uh should

play17:53

be the way to go and how do you know you

play17:55

how do you strike the perfect balance

play17:57

between ENT test unit test and

play18:00

integration test in the front

play18:02

end uh that's a very very good question

play18:05

um I would

play18:07

say you need to understand if we write a

play18:10

lot of entend tests but we have imagine

play18:11

no unit test and integration test what

play18:14

will happen is that when your entry test

play18:15

fail fails it it's good you know that

play18:18

there is a bug but it will be extremely

play18:19

hard to go through the code and find

play18:20

exactly where it is so the more unit an

play18:23

integration test you have the easier it

play18:25

is when you find a bug to localize the

play18:27

when you make root call analysis to

play18:29

localize the the bug boundary and really

play18:31

eliminate all the other sides of the

play18:33

application if you only write 10 to0

play18:35

test uh you will have coverage for your

play18:37

features but when something breaks it

play18:39

will still take you hours to debug so I

play18:41

would say uh I would be smart about it

play18:43

and understand your application and then

play18:45

have a have a certain balance I cannot

play18:47

give you exact numbers but I would write

play18:50

a lot of unit test and a lot of ENT test

play18:52

to finish the feat to cover the features

play18:55

and strategically some integration test

play18:57

on on the critical features like if you

play18:58

have login or if you have payments you

play19:01

got to see also like what's more

play19:02

critical in your

play19:04

product uh you did mention the word code

play19:07

coverage when you were talking about

play19:09

unit testing um you know can you tell me

play19:12

more about code coverage like just

play19:14

shortly what it is and um what would be

play19:17

the appropriate amount of code coverage

play19:20

in your opinion in a fronted

play19:23

application uh so yeah code coverage

play19:26

it's the amount of code that EX executes

play19:29

when we run the test basically so people

play19:31

say it's the amount of code we cover in

play19:33

we cover it with test but it's actually

play19:35

the amount of code that actually

play19:36

executes when you onun the test um in

play19:39

the front end I would say it's a bit

play19:40

more tricky than in the back end where

play19:42

it's a bit easier but I would try to

play19:44

keep it all the way up to 60% whenever

play19:47

possible if you go under 60 then it will

play19:50

be quite unpredictable and the test are

play19:52

unusable because you can't really trust

play19:54

um there's a lot of code that's not

play19:56

tested so the test might fail but you

play19:58

don't really know why do I failing so 60

play20:00

to 70% would be good I think going over

play20:02

80 to 90 it's a bit of an Overkill and

play20:04

you didn't get a lot of value from those

play20:06

marginal test um in your last uh Team or

play20:09

company you know what was the code

play20:12

coverage you were aiming for and you

play20:14

know did you had any anecdotes anything

play20:17

that you can tell us how did it went for

play20:20

you um yeah sure so at least in some of

play20:23

my roles I worked in the finance

play20:24

industry where they they made it was a

play20:27

general rule for the company to have

play20:28

around 9 5% of code coverage uh we ended

play20:31

up writing a lot of test that made

play20:32

literally no sense um you you have a lot

play20:35

of files that sometimes are just

play20:36

explicit they decare types you had to

play20:39

exclude all those and it was pretty much

play20:41

of a hard measure everybody was saying

play20:43

they do tdd but in the end everybody go

play20:46

test before pushing their PR um so I'll

play20:48

be really careful with these measures

play20:50

because in the end people say they would

play20:51

do it and so on but uh but yeah I had

play20:53

that experience I also been in teams

play20:55

that didn't wrote any test at all to be

play20:56

very honest there was uh CS that was a

play20:59

disaster and I would say the perfect

play21:01

balance is in the front end 60 to 70 in

play21:03

the back end I would go a bit more up 90

play21:06

to 80 80 to 90

play21:08

95 uh it's still doable because the back

play21:11

end again it's it's a bit more

play21:12

functional and it's sometimes much

play21:14

easier to un need to test or to

play21:15

modularize things and it is in the front

play21:17

end cool now um let's talk finally final

play21:22

uh final category we will talk about web

play21:25

performance right um and question here

play21:28

is

play21:29

can you explain

play21:30

FCP okay what are the causes of a bad

play21:33

FCP score okay so FCP FCP would be the

play21:38

first contentful paint uh which is

play21:41

basically the time it takes since the

play21:43

user hits the uh the enter button in the

play21:46

browser to display something and to

play21:48

display anything to them um and usually

play21:52

it's really bad if you ship a lot of

play21:55

JavaScript that is client side Ender so

play21:57

it takes a lot of time to part all this

play21:59

react bundle and interpret it for the

play22:01

browser to show something or you don't

play22:03

use a CDN so maybe the assets are not

play22:06

compressed or they're not cached and so

play22:08

it it literally just takes a lot of time

play22:10

to to do that or you have a lot of CSS

play22:13

uh CSS CSS in the header uh has to be

play22:17

all interpreted before we move on to

play22:19

JavaScript and finally start

play22:21

incrementally rendering the HTML um so

play22:24

if you have a lot of that it will also

play22:25

take a long time so these are three CES

play22:27

that I've seen in the past

play22:29

that's the first thing I would look at

play22:30

let's suppose you take charge of a front

play22:33

application you run your um analysis

play22:36

whatever tool you use Lighthouse For

play22:38

example and you realize that the FCP it

play22:40

has a very low FCP score how would you

play22:44

go about fixing

play22:47

it uh so okay so we saw that the FCP is

play22:50

low on lious I definitely r a couple of

play22:52

analyses just to make sure the score is

play22:53

legit and once we have that uh I

play22:56

probably as I mentioned look first into

play22:57

the CDN it's usually easiest thing to do

play22:59

and gets a lot of benefits if there is

play23:01

not one set um and then probably I would

play23:04

uh use something to inspect the bundle

play23:07

and see if we can remove any part of the

play23:09

JavaScript that's not being used and if

play23:11

that doesn't still work I will look into

play23:13

code splitting and really only load the

play23:16

JavaScript that you need for a specific

play23:17

page I'll probably talk to the product

play23:19

managers and see which are the pages

play23:21

that are more critical for us to to load

play23:23

fast usually it's not all the pages and

play23:26

I would focus on those ones and load

play23:28

split to make sure that uh we really

play23:30

load as little JavaScript as possible

play23:34

and maybe defer some some of the

play23:36

JavaScript or a sync loaded uh you can

play23:38

do that with weback uh with react. lazy

play23:41

also you can split your component Tre uh

play23:44

so basically code splitting would be one

play23:45

of the biggest um things I will do after

play23:48

we add the CDN and we make sure we have

play23:50

caching and compression in place so

play23:52

number one CDN caching compression

play23:55

number two remove um whatever Li lies

play23:58

that you can remove that are adding way

play24:00

too much JavaScript to the bundle uh

play24:02

number three code splitting and and

play24:04

shipping different chunks of JavaScript

play24:07

as you need them right to make that

play24:08

lower have you considered and when do

play24:11

you consider something like server side

play24:13

rendering let's suppose this is a react

play24:15

client side um client side rendered

play24:18

application and you've went with all the

play24:20

optimizations and still because of the

play24:22

size of the application the application

play24:24

is too big and the FCP it's way too slow

play24:27

um would you consider server side

play24:29

rendering what would you know would that

play24:32

improve the situation or

play24:34

not um server side rendering when done

play24:37

well it will definitely improve uh by

play24:40

basically removing by basically yeah

play24:41

showing instantly we ship the render

play24:44

HTML to the client so they will get

play24:46

something immediately they'll see

play24:47

something uh it does add however

play24:50

complexity to your code base so I know

play24:52

in some use cases where people rushed

play24:54

into Ser side then they had to roll it

play24:56

back I would definitely look at our

play24:57

product and if the product is very

play24:59

sensitive to loading speed or sensitive

play25:01

to SEO then you'll get a lot of benefits

play25:03

from server side gring but if it's not

play25:05

if it's like a sass application where

play25:07

it's like accounting software then

play25:09

there's usually no value in serite

play25:11

rendering so you would stick to CSI

play25:13

still because it's not yes yeah probably

play25:16

in e-commerce in something like

play25:17

e-commerce or the media where you have a

play25:19

newspaper they want to load very fast

play25:21

and they're very sensitive to SEO

play25:22

they're probably most of them using

play25:25

serviceing asome ban this was it for

play25:28

today we uh covered a lot of topics from

play25:31

CSS injs to react react hooks to weback

play25:36

to tooling to testing and now on to

play25:39

Performance I hope you um folks enjoy

play25:42

this we um we're going to publish some

play25:45

more series on this thank you B for this

play25:47

one and we will see you in the next

play25:50

one see you in the next one

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
Frontend InterviewWebpackCSS-in-JSReact HooksPerformance OptimizationCode SplittingTree ShakingError BoundariesState ManagementTesting Strategies
¿Necesitas un resumen en inglés?