Latest / CyberSecurity Summary / React Key Concepts: An in-depth guide to React's core features
Transcript
- 0:00Speaker 1: Welcome to the deep dive, where we slice through information
- 0:03Speaker 1: overload to bring you the most important insights from your sources.
- 0:07Speaker 1: Today we're diving deep into reacts core features, drawing from
- 0:10Speaker 1: REACT key concepts and in depth guide to reacts core features.
- 0:14Speaker 1: Our mission to sort of demystify the art of building
- 0:17Speaker 1: dynamic user interfaces from the absolute fundamentals all the way
- 0:21Speaker 1: to cutting edge full stack capabilities. Whether you're a curious learner,
- 0:25Speaker 1: just starting out, or gearing up for a big technical meeting,
- 0:29Speaker 1: we're here to deliver those aha moments and clear actionable insights. Okay,
- 0:34Speaker 1: let's unpack this React as a huge topic. Imagine we're
- 0:37Speaker 1: trying to build some interactive websites starting fresh. What kind
- 0:40Speaker 1: of problems pop up if you try to use just
- 0:42Speaker 1: planal JavaScript? Why not stick with that?
- 0:45Speaker 2: Right? Well, the big challenge with planin JavaScript, especially for
- 0:47Speaker 2: complex UIs, is what we call it's imperative approach.
- 0:51Speaker 1: Imperative meaning you're telling.
- 0:52Speaker 2: The computer every single step to take. Yeah, like construction
- 0:55Speaker 2: after instruction, detailing exactly how to change the UI.
- 0:58Speaker 1: Okay, micromanaging the brain pretty much, and even for fairly
- 1:02Speaker 1: simple things, this can balloon.
- 1:03Speaker 2: You might hit it over underlines the code, and maybe
- 1:06Speaker 2: three quarters of that is just fiddling directly with the dom,
- 1:09Speaker 2: managing its state, all that boilerplate stuff. So naturally this
- 1:13Speaker 2: makes Vanilla JS pretty error prone and honestly just a
- 1:17Speaker 2: huge time sink. It's like being a puppeteer, you know,
- 1:20Speaker 2: pulling every single string.
- 1:22Speaker 1: Yeah, that puppeteer analogy really clicks. Okay, So if that's
- 1:25Speaker 1: the old how what reacts? What what's the philosophy shift?
- 1:29Speaker 2: The shift with React is moving from puppeteer to architect.
- 1:33Speaker 2: Instead of telling the browser how to move every pixel,
- 1:36Speaker 2: you just declare the final blueprint. You focus on the
- 1:38Speaker 2: big picture, the desired end state.
- 1:41Speaker 1: Ah.
- 1:42Speaker 2: So like if a button is clicks, you just tell
- 1:43Speaker 2: React okay, the tech should now be this, and React
- 1:46Speaker 2: figures out the how efficiently it keeps the paragraph updated automatically, so.
- 1:50Speaker 1: It handles the translation exactly.
- 1:52Speaker 2: You describe your ideal UI. React handles the MESSI dom stuff.
- 1:56Speaker 2: It frees you up to focus on your actual business logic,
- 1:59Speaker 2: which is I mean a massive productivity boost.
- 2:01Speaker 1: So for US developers it means letting React handle those
- 2:05Speaker 1: low level dawn interactions. That sounds like a relief. How
- 2:09Speaker 1: do we actually get started. Set up a project good question.
- 2:13Speaker 2: To get going, You typically use a tool like vite.
- 2:16Speaker 2: You run NPM create vite at latest my app in
- 2:19Speaker 2: your terminal than NPM.
- 2:20Speaker 1: Installed Fight Why fyte specifically for learning, It's often recommended
- 2:24Speaker 1: because it's lightweight, super fast.
- 2:26Speaker 2: Frameworks like next dot gs are powerful, absolutely, but they
- 2:30Speaker 2: add their own concepts, their own syntax, which can be
- 2:32Speaker 2: a bit overwhelming.
- 2:33Speaker 1: Right at the start, makes sense, keep it simple initially, right, and.
- 2:36Speaker 2: In that new project, you'll see files like app dot
- 2:38Speaker 2: jsx and main dojsx. That dot jsx just tells you
- 2:42Speaker 2: these files mixed JavaScript with that HTML like syntax called JSX.
- 2:47Speaker 1: Got it JSX, okay, project setup. Now inside the code.
- 2:50Speaker 1: React apps are built with components. What makes them so
- 2:53Speaker 1: well fundamental components.
- 2:54Speaker 2: Are basically reacts, reusable building blocks. Think of them like
- 2:58Speaker 2: specialized Lego bricks. Okay, its own javascrip logic. Often it's
- 3:02Speaker 2: CSS styling too, and it's JSX markup. All self contained.
- 3:06Speaker 2: This makes your code way more manageable, promotes reuse, and how.
- 3:09Speaker 1: Does the browser actually see these?
- 3:11Speaker 2: And that main dot JSX file. There's a bit of
- 3:13Speaker 2: code using reactums, specifically root dot render that's the match
- 3:18Speaker 2: cup part. It takes your JSX components and translates them
- 3:21Speaker 2: into the actual dom instructions the browser understands.
- 3:24Speaker 1: So the index dot html file is just empty much
- 3:26Speaker 1: just a shell.
- 3:27Speaker 2: Yeah, React fills it all in Oh and a neat detail.
- 3:31Speaker 2: Custom components they always start with an upper case letter
- 3:34Speaker 2: pascal case like submit button.
- 3:37Speaker 1: Why is that?
- 3:38Speaker 2: React needs it to tell your custom components apart from
- 3:41Speaker 2: standard HTML tags like div or p when it parses
- 3:44Speaker 2: the JSX ah.
- 3:45Speaker 1: Okay, good tip. And how do you decide what should
- 3:48Speaker 1: be a component?
- 3:49Speaker 2: A good rule of thumb is to think about your data.
- 3:51Speaker 2: Try to create a separate component for every distinct thing
- 3:54Speaker 2: or data entity you can identify, like an individual to
- 3:57Speaker 2: do item in a list. That's a component the list
- 4:00Speaker 2: another component?
- 4:01Speaker 1: Okay, components is building blocks? Great for structure, but how
- 4:04Speaker 1: do we make them? You know dynamics, so they aren't
- 4:06Speaker 1: all identical? How do we pass in different information?
- 4:10Speaker 2: Right? Static blocks aren't very useful. That's where props come in.
- 4:13Speaker 2: Props short for properties, are how you can figure your components.
- 4:17Speaker 2: You pass data down into them, How you pass them
- 4:19Speaker 2: You can pass them like attributes in htmail. Right on
- 4:22Speaker 2: the component tag or even as children between the opening
- 4:25Speaker 2: and closing tags. React automatically bundles these into a props
- 4:30Speaker 2: object that gets passed into your component function.
- 4:32Speaker 1: So inside the component I just use propstock whatever you can.
- 4:36Speaker 2: Yeah, like props dot title, But often developers use a
- 4:39Speaker 2: technique called destructuring right in the function parameters structure. It's
- 4:43Speaker 2: like unpacking the props object immediately. So instead of props
- 4:46Speaker 2: dot title, you just have title. Makes the function definition cleaner,
- 4:51Speaker 2: like function product, title, price. You see exactly what that
- 4:55Speaker 2: component expects.
- 4:56Speaker 1: Let's typing definitely.
- 4:58Speaker 2: Yeah, And there's also this spread up operator. If you
- 5:01Speaker 2: have a JavaScript object with lots of settings, you can
- 5:04Speaker 2: use dot can fee and your JSX to pass all
- 5:06Speaker 2: those object properties as individual props. Really handy for rapper components.
- 5:10Speaker 1: Okay, props seem powerful, but you mentioned a challenge earlier,
- 5:13Speaker 1: prop drilling.
- 5:14Speaker 2: Ah, yes, prop drilling. It's well, it's a bit of
- 5:18Speaker 2: a pain point sometimes.
- 5:19Speaker 1: What is it exactly?
- 5:20Speaker 2: It's when you have to pass a prop down through
- 5:22Speaker 2: multiple layers of nested components, even if the components in
- 5:26Speaker 2: the middle don't actually use that prop themselves, they just
- 5:28Speaker 2: act as carriers.
- 5:30Speaker 1: Like passing a note in class.
- 5:31Speaker 2: Exactly like that. The components in the middle just passed
- 5:34Speaker 2: along until it reaches the component way down the chain
- 5:36Speaker 2: that actually needs the data.
- 5:38Speaker 1: And why is that bad?
- 5:39Speaker 2: It makes the code less reusable, adds clutter makes refactoring harder.
- 5:44Speaker 2: If you decide to change how data flows, you might
- 5:46Speaker 2: have to edit files for components that weren't even directly involve.
- 5:50Speaker 2: It often suggests your state might be better managed elsewhere.
- 5:54Speaker 1: Hmm, okay, avoid the note passing if possible, so props
- 5:57Speaker 1: handle configuration. But how do we make UIs actually react
- 6:01Speaker 1: to things the user does clicks typing?
- 6:05Speaker 2: Excellent question. This is where state becomes crucial. State is
- 6:08Speaker 2: managed primarily using another fundamental React tool, the U state
- 6:13Speaker 2: hook A hook.
- 6:14Speaker 1: Yeah.
- 6:14Speaker 2: Hooks are special functions that let you hook into React features.
- 6:18Speaker 2: U state is the main one for adding state to
- 6:20Speaker 2: functional components.
- 6:21Speaker 1: And what is state in this context?
- 6:24Speaker 2: Think of state as any data within a component that,
- 6:27Speaker 2: when it changes, should cause React to re render that
- 6:31Speaker 2: component and update the UI. It's the memory of the component.
- 6:35Speaker 1: Okay, so how does you state work?
- 6:38Speaker 2: When you call U state you usually pass an initial value.
- 6:41Speaker 2: It then returns in array with exactly two things. First,
- 6:45Speaker 2: the current state value itself, and second a special function
- 6:48Speaker 2: you use to update that value.
- 6:50Speaker 1: So a value and a function to change it precisely.
- 6:53Speaker 2: And here's the key react nose to reevaluate or rerun
- 6:57Speaker 2: your component function only when that state updating function gets called.
- 7:01Speaker 2: That's the trigger for UI updates.
- 7:03Speaker 1: What if I have multiple pieces of data that need
- 7:05Speaker 1: to change.
- 7:05Speaker 2: Good point, you have options. You can call U state
- 7:08Speaker 2: multiple times for each piece of state, creating multiple state slices,
- 7:12Speaker 2: or you could manage a single larger state object with
- 7:14Speaker 2: U state.
- 7:15Speaker 1: Is one better than the other depends multiple use?
- 7:18Speaker 2: State calls are often simpler for unrelated pieces of data
- 7:21Speaker 2: if you use a single object, though, you have to
- 7:23Speaker 2: be careful when updating. When you call the state updating
- 7:25Speaker 2: function with a new object, it completely replaces the old one,
- 7:29Speaker 2: so you need to make sure your new object includes
- 7:31Speaker 2: all the properties, even the ones that didn't change. Usually
- 7:34Speaker 2: you use the spread operator to copy the old state first.
- 7:37Speaker 1: Ah okay, got to remember to copy the old stuck.
- 7:41Speaker 2: Exactly And another best practice. If your new state depends
- 7:44Speaker 2: on the previous state value like incrementing a counter, you
- 7:48Speaker 2: should pass a function to the state setter like set
- 7:51Speaker 2: counterpreve counterpost count plus one. This ensures you're working with
- 7:55Speaker 2: the latest value, especially if updates happen quickly.
- 7:58Speaker 1: Got it? Use the function form for updates based on
- 8:01Speaker 1: previous state. What about calculating things from state?
- 8:04Speaker 2: You can just derive values. If you have user input
- 8:06Speaker 2: in state, you can calculate its length directly in your
- 8:09Speaker 2: component cont snumb chars user input dot length. No need
- 8:13Speaker 2: for separate state for that derived value.
- 8:15Speaker 1: Okay, And what if multiple components need the same state,
- 8:19Speaker 1: like that prop drilling problem.
- 8:21Speaker 2: That's where the pattern of lifting state up comes in.
- 8:23Speaker 2: You find the closest common ancestor component of all the
- 8:26Speaker 2: components that need the state, and you manage the state there.
- 8:29Speaker 1: So the parent component holds the state right, and.
- 8:32Speaker 2: Then it passes the state value down as props and
- 8:35Speaker 2: passes the state updating function down as props too, so
- 8:38Speaker 2: the child components can still read and update the shared state.
- 8:42Speaker 2: It centralizes control.
- 8:44Speaker 1: Lifting state up makes sense. Okay, we have components props state.
- 8:48Speaker 1: Now how do we handle dynamic displays showing or hiding things,
- 8:53Speaker 1: rendering lists?
- 8:54Speaker 2: Yeah, very common. Need for conditional content like showing an
- 8:57Speaker 2: error message only when there's an error or a life
- 9:00Speaker 2: oding spinner. You can just use standard JavaScript logic right
- 9:02Speaker 2: inside your JSX.
- 9:03Speaker 1: Like if statements yep.
- 9:04Speaker 2: If statements work fine outside the return inside JSX. Ternary
- 9:08Speaker 2: expressions conditioning JSX one true, JSEX one false are super common,
- 9:12Speaker 2: or the logical A and D operator conditioned JSX one true.
- 9:15Speaker 1: Okay.
- 9:16Speaker 2: You can even store ENTIREJX structures and variables and render
- 9:19Speaker 2: them conditionally, or even change the HTML tag itself based
- 9:22Speaker 2: on a prop, like rendering a button or a tag
- 9:24Speaker 2: dynamically flexible.
- 9:26Speaker 1: What about lists of data like an array of items?
- 9:28Speaker 2: For lists, you'll almost always use the JavaScript dot map method.
- 9:33Speaker 2: You take your array of data, map over it, and
- 9:35Speaker 2: return a JSX element for each item. Simple enough, but
- 9:40Speaker 2: there's a very common warning. You'll hit React complaining about
- 9:44Speaker 2: missing keys.
- 9:45Speaker 1: Keys. What are those?
- 9:46Speaker 2: Keys? Are special unique identifiers you need to add as
- 9:49Speaker 2: a prop to each element you render inside a list
- 9:52Speaker 2: map like like key, item, dot id.
- 9:54Speaker 1: Why does React need them?
- 9:56Speaker 2: They help React identify which items in the list have changed,
- 9:59Speaker 2: been added, or removed. Without keys, React might have to
- 10:02Speaker 2: re render the entire list unnecessarily. With keys, it can
- 10:05Speaker 2: be much smarter even just rearranging existing dom elements. If
- 10:09Speaker 2: the order changes. It's a performance optimization.
- 10:12Speaker 1: And the key has to be unique.
- 10:13Speaker 2: Unique among good siblings in that list. Yes, yeah, and stable.
- 10:16Speaker 2: It shouldn't change between renders for the same item. That's
- 10:19Speaker 2: why using the array index is a key is often
- 10:21Speaker 2: a bad idea, especially if the list can be reordered
- 10:24Speaker 2: or items removed from the middle. The index doesn't stick
- 10:27Speaker 2: with the data item. Use a unique id from your
- 10:29Speaker 2: data if possible.
- 10:31Speaker 1: Got it? Use stable unique IDs for keys. And you
- 10:35Speaker 1: mentioned immutability before with state objects, does that apply to
- 10:39Speaker 1: a rays in the state too?
- 10:40Speaker 2: Absolutely? If you have an array in state and you
- 10:42Speaker 2: want to add an item, you must not use methods
- 10:45Speaker 2: like dot push that modify the original array. Why not
- 10:49Speaker 2: because React to text changes by comparing objector ray references.
- 10:52Speaker 2: If the reference hasn't changed because you modified it in place,
- 10:55Speaker 2: React won't see the change and won't re render.
- 10:57Speaker 1: So what should I do?
- 10:58Speaker 2: You need to create a new array. Use methods like
- 11:00Speaker 2: concat or more commonly, the spread operators at my array
- 11:04Speaker 2: dot old array new item. This creates a brand new
- 11:06Speaker 2: array reference, signaling the change to React.
- 11:09Speaker 1: Always immutable updates okay, immutable updates for objects and a
- 11:13Speaker 1: raised and state crucial now styling. How do we make
- 11:16Speaker 1: our components look good without CSS rules bleeding everywhere?
- 11:20Speaker 2: Styling? Right, you can just in court standard CSS files
- 11:22Speaker 2: import a component on CSS. But like you said, those
- 11:25Speaker 2: styles are global by default, easy to get accidental clashes,
- 11:28Speaker 2: especially in larger apps.
- 11:30Speaker 1: So what are the alternatives?
- 11:31Speaker 2: You can use inline styles in JSX you use the
- 11:33Speaker 2: style prop, but it expects a JavaScript object, not a string,
- 11:38Speaker 2: so it looks like style color red, padding top tenpx.
- 11:42Speaker 2: Notice the cambl case for CSS.
- 11:44Speaker 1: Properties inline styles. Okay, what about CSS classes?
- 11:47Speaker 2: The CSS classes you use the class name prop, not
- 11:50Speaker 2: class because class is a reserved word in JavaScript.
- 11:53Speaker 1: Ah, class name, got it, And you.
- 11:55Speaker 2: Can set class name dynamically based on state or props,
- 11:58Speaker 2: which is really useful for conditionals.
- 12:00Speaker 1: But that still doesn't solve the global clash problem if
- 12:03Speaker 1: we import CSS files right correct?
- 12:06Speaker 2: For that, CSS modules are a great solution. You name
- 12:08Speaker 2: your CSS file like my component dot module dot css.
- 12:12Speaker 2: When you import it, the build process automatically transforms your
- 12:15Speaker 2: class names into unique strings, effectively scoping them to that component.
- 12:19Speaker 1: So title in one component won't clash with dot title
- 12:22Speaker 1: in another.
- 12:22Speaker 2: Exactly, it becomes something like my component title A three
- 12:25Speaker 2: to seven B prevents clashes entirely. Other popular options include
- 12:29Speaker 2: CSS and JS libraries like style components, which let you
- 12:33Speaker 2: write actual CSS inside your JavaScript files, often using tag
- 12:37Speaker 2: template literals, and utility first libraries like Tail and thess
- 12:41Speaker 2: are also very popular, giving you lots of small, reusable classes.
- 12:44Speaker 1: Lots of options there. Okay, what if? What if I
- 12:47Speaker 1: really need to directly touch a DOM element, like to
- 12:50Speaker 1: focus an input field or measure its size. Escape reacts
- 12:53Speaker 1: control for a moment.
- 12:54Speaker 2: Sometimes you do need that. That's where refs come in,
- 12:57Speaker 2: using the use ref another hook yep use ref lets
- 13:00Speaker 2: you create a reference, a sort of pointer to a
- 13:02Speaker 2: value that persists across renders. Commonly, you attach this ref
- 13:06Speaker 2: to a DOM element using the ref prop like input
- 13:08Speaker 2: ref my input ref, and then then inside your component logic,
- 13:12Speaker 2: my input ref dot current will hold the actual DOM
- 13:14Speaker 2: node and you can call methods on it like my
- 13:17Speaker 2: inputref dot current dot focus.
- 13:19Speaker 1: Does changing the ref cause a rerender like state No.
- 13:22Speaker 2: Crucially it doesn't. Updating a ref my input ref dot
- 13:27Speaker 2: current ocals some value does not trigger a component rerender.
- 13:30Speaker 2: That's the key difference from state use res for values
- 13:34Speaker 2: you need to track, but which don't directly affect the
- 13:36Speaker 2: visual output.
- 13:37Speaker 1: Okay. Refs for direct dom access or persistent values without rerenders.
- 13:42Speaker 2: And this leads to the idea of controlled versus uncontrolled components,
- 13:45Speaker 2: especially for.
- 13:46Speaker 1: Forms control versus uncontrolled.
- 13:48Speaker 2: A controlled input component has its value managed by React
- 13:51Speaker 2: state use state. Every keystroke updates the state, which updates
- 13:55Speaker 2: the input's value. Prop React is fully in control okay.
- 13:59Speaker 2: An uncontrolled input doesn't have its value tied to state.
- 14:02Speaker 2: You'd use a rapt to read its value only when needed,
- 14:04Speaker 2: maybe on form submission. React doesn't manage its value moment
- 14:07Speaker 2: to moment.
- 14:08Speaker 1: Which one should I use?
- 14:09Speaker 2: Generally prefer controlled components. It keeps React as the single
- 14:12Speaker 2: source of truth. Uncontrolled can be simpler for very basic forms,
- 14:16Speaker 2: but controlled gives you more power for validation, dynamic changes,
- 14:20Speaker 2: et cetera.
- 14:20Speaker 1: Got it, Prefer controlled? Now one more tricky layout thing, modals,
- 14:27Speaker 1: pop ups, tooltips, things that need to appear on top
- 14:30Speaker 1: but might be deep in the component tree. How does
- 14:33Speaker 1: React handle that?
- 14:34Speaker 2: Ah? Yes, the z index nightmare For that. React offers portals.
- 14:38Speaker 2: You use the create portal function from React.
- 14:40Speaker 1: Tom portals like teleporting UI.
- 14:43Speaker 2: Kind of create portal takes two arguments the JSX you
- 14:47Speaker 2: want to render like your modal component and a reference
- 14:50Speaker 2: to a domnode outside your component's current place in the
- 14:52Speaker 2: dom tree where you want to render it up inside
- 14:54Speaker 2: where typically you'd have an empty DIV somewhere near the
- 14:57Speaker 2: root of your index dot HTML like div ideo modal RUDDIV.
- 15:01Speaker 2: You tell the portal to render your modal inside that div.
- 15:04Speaker 1: So even if my button triggering the modal is nested
- 15:06Speaker 1: deep down.
- 15:07Speaker 2: Exactly, the modal renders visually in that top level DIV,
- 15:10Speaker 2: avoiding CSS decking context issues and making styling much easier.
- 15:14Speaker 2: But logically it still behaves like a child of the
- 15:16Speaker 2: component that created the portal, regarding event bubbling in context.
- 15:19Speaker 1: It's clever portals. Okay, that solves a common headache. Now
- 15:23Speaker 1: let's shift gears slightly. We've talked rendering state. What about
- 15:28Speaker 1: actions that happen besides rendering, like fetching data, setting timers,
- 15:33Speaker 1: interacting with browser APIs.
- 15:35Speaker 2: Right, Those are called side effects. There are actions or
- 15:37Speaker 2: processes that happen in addition to the main rendering process.
- 15:41Speaker 2: Sending HTTP requests, using local storage, setting up timers or
- 15:45Speaker 2: subscriptions all side effects.
- 15:47Speaker 1: And can they cause problems?
- 15:48Speaker 2: Oh? Definitely. A classic newbie mistake is putting a side
- 15:51Speaker 2: effect directly in the component body that also updates state.
- 15:55Speaker 2: If fetching data updates state and the component rerenders, it
- 15:59Speaker 2: might fetch data, get update state again, infinite loop.
- 16:02Speaker 1: Yikes. How do we avoid that with another hook?
- 16:05Speaker 2: That's effect.
- 16:06Speaker 1: Yeah.
- 16:06Speaker 2: Its whole purpose is to manage side effects safely within
- 16:09Speaker 2: functional components.
- 16:10Speaker 1: Okay, how does use effect work?
- 16:12Speaker 2: It takes a function, your effect function, containing the side
- 16:14Speaker 2: effect code, and crucially a second argument, a dependency's.
- 16:18Speaker 1: Array dependencies array.
- 16:19Speaker 2: Yes, this array tells React when to rerun your effect function.
- 16:24Speaker 2: If you provide an empty array, the effect runs only once,
- 16:27Speaker 2: right after the component mounts renders for the first time.
- 16:30Speaker 1: Only once useful for initial data fetching exactly.
- 16:34Speaker 2: If you list variables or props in the array, like
- 16:37Speaker 2: user id, the effect will run after the first render
- 16:40Speaker 2: and anytime the value of user ed changes between renders.
- 16:43Speaker 1: Ah, so it reruns only if specific dependencies change.
- 16:47Speaker 2: Precisely. If you don't provide the array at all, which
- 16:50Speaker 2: is generally discouraged, the effect runs after every render. That's
- 16:53Speaker 2: often how those infinite loops start.
- 16:55Speaker 1: Okay, so always provide the dependency array mpty for once
- 16:59Speaker 1: or list the values used inside the effect.
- 17:01Speaker 2: That's the golden rule. Add all values props, state variables
- 17:06Speaker 2: from the component scope that are used inside your effect
- 17:09Speaker 2: function to the dependency's rate.
- 17:10Speaker 1: All of them, any exceptions.
- 17:11Speaker 2: The main exception is state updating functions the second thing
- 17:14Speaker 2: returned by U state React guarantees these functions themselves never change,
- 17:19Speaker 2: so you don't need to list them as dependencies. Same
- 17:22Speaker 2: for built in APIs like fetch or refs.
- 17:24Speaker 1: Okay, what about cleaning up like clearing a timer?
- 17:27Speaker 2: Use effect candles that too. If your affect function returns
- 17:30Speaker 2: another function, React will run that return function as a
- 17:34Speaker 2: cleanup function.
- 17:35Speaker 1: When does it run the cleanup it.
- 17:36Speaker 2: Runs before the effect runs again if dependencies change, and
- 17:39Speaker 2: also when the component unmounts is removed from the UI.
- 17:43Speaker 2: Perfect for clearing timers, removing event listeners, unsubscribing from things.
- 17:48Speaker 1: Very useful. What if a dependency is a function to
- 17:51Speaker 1: find inside my component? Won't that cause the effect to
- 17:54Speaker 1: run too often?
- 17:55Speaker 2: Good catch. If you define a function inside your component,
- 17:58Speaker 2: it gets recreated on every render. If that function is
- 18:01Speaker 2: a dependency of use effect, the effect will run every time.
- 18:05Speaker 1: So how do we fix that?
- 18:06Speaker 2: We use another hook, use callback. You wrap your function
- 18:10Speaker 2: definition and use callback and provide its dependencies. Use callback
- 18:14Speaker 2: will then return a memoized version of your function, which
- 18:16Speaker 2: only changes if its dependencies change.
- 18:19Speaker 1: So use callback memois as functions preventing unnecessary effect runs exactly.
- 18:23Speaker 2: It works hand in hand with use effect in those cases.
- 18:25Speaker 1: Okay, effects cleanup dependencies. That covers side effects. What if
- 18:31Speaker 1: state management gets really complex, multiple related pieces of state
- 18:36Speaker 1: complex update logic.
- 18:37Speaker 2: When use state starts feeling cumbersome for a related state,
- 18:40Speaker 2: React offers an alternative, the use reducer hook.
- 18:44Speaker 1: Use reducer sounds like redux.
- 18:46Speaker 2: It's inspired by similar patterns. Yeah, it's better suited for
- 18:49Speaker 2: managing complex state logic. You define a reducer function separate
- 18:53Speaker 2: from your component. This function takes the current state and
- 18:56Speaker 2: an action object and returns the new state actions. Yeah.
- 18:59Speaker 2: Instead of calling a state center directly, you dispatch action
- 19:02Speaker 2: objects to your reducer like type increment payload. The reducer
- 19:07Speaker 2: handles the update logic based on.
- 19:09Speaker 1: The action type, So use reducer returns.
- 19:11Speaker 2: It returns the current state and that dispatch function. So
- 19:13Speaker 2: constate dispatch us reducer initial state. It helps centralize and
- 19:17Speaker 2: organize complex state transitions.
- 19:20Speaker 1: Okay, use reducer for complex state. Now what about reusing logic.
- 19:23Speaker 1: If I have some stateful logic, maybe involving state and
- 19:26Speaker 1: effects that I want to use in multiple components, perfect
- 19:28Speaker 1: use case for custom hooks. Custom hooks.
- 19:31Speaker 2: Yeah, these are just regular JavaScript functions, but their name
- 19:34Speaker 2: must start with use, like use fetch or use counter.
- 19:37Speaker 2: Inside a custom hook, you can call other React hooks
- 19:40Speaker 2: like use state, use effect, user ref, etc.
- 19:43Speaker 1: So it's like bundling up hook logic exactly.
- 19:46Speaker 2: You encapsulate stateful logic into a reusable function. Any component
- 19:50Speaker 2: can then call your custom hook like const data I
- 19:53Speaker 2: loading use fetch.
- 19:55Speaker 1: Does every component share the same state from the custom hook? No?
- 19:59Speaker 2: And this is key. Every component that calls your custom
- 20:02Speaker 2: hook gets its own independent instance of the state and
- 20:05Speaker 2: effects defined within that hook. It's reusable logic, but isolated
- 20:09Speaker 2: state per component instance.
- 20:10Speaker 1: Ah. That's powerful custom hooks for reusable stateful logic. Okay,
- 20:15Speaker 1: let's talk performance. We build these complex apps, how to
- 20:18Speaker 1: keep them fast? How does React help and what can
- 20:20Speaker 1: we do?
- 20:20Speaker 2: Performance is crucial? Definitely reacts. First big optimization is the
- 20:24Speaker 2: virtual dom we touched.
- 20:25Speaker 1: On earlier, right, the in memory copy.
- 20:27Speaker 2: Yeah, instead of hitting the real browser dom. For every
- 20:30Speaker 2: little change, which is slow, React builds up changes in
- 20:34Speaker 2: its fast in memory virtual dom. It then compares the
- 20:38Speaker 2: new virtual dom state with.
- 20:39Speaker 1: The previous one, finds the differences.
- 20:41Speaker 2: Right, It performs a diffing algorithm to find the minimal
- 20:44Speaker 2: set of changes needed. Then it applies only those changes
- 20:48Speaker 2: to the actual dom in one efficient batch. It minimizes
- 20:52Speaker 2: direct dom manipulation.
- 20:54Speaker 1: Okay, virtual dom diffing. What else?
- 20:57Speaker 2: State batching is another built in when if you call
- 20:59Speaker 2: set state multiple times in quick succession within the same
- 21:03Speaker 2: event handler or effect, React is smart enough to bash
- 21:06Speaker 2: those updates together into a single rerender cycle.
- 21:09Speaker 1: So it doesn't re render multiple times unnecessarily exactly.
- 21:13Speaker 2: It waits, collects all the state updates for that tick,
- 21:16Speaker 2: and then performs one single rerender reflecting all those changes.
- 21:19Speaker 2: Saves a lot of wasted work.
- 21:20Speaker 1: That's good, But what if a component itself rerenders even
- 21:24Speaker 1: if it's props didn't change just because it's parent re rendered.
- 21:26Speaker 2: That can happen, and it can be a performance bottleneck.
- 21:29Speaker 2: To prevent those unnecessary child component evaluations. React gives us
- 21:33Speaker 2: React dot memo. You wrap your entire component export with
- 21:37Speaker 2: memo like export default React dot memo my component. What
- 21:42Speaker 2: memo does is perform a shallow comparison of the component's
- 21:45Speaker 2: props between renders. If the props haven't changed, React skips
- 21:50Speaker 2: re rendering that component and its entire subtree.
- 21:52Speaker 1: So it stops the rerender cascade.
- 21:54Speaker 2: Precisely, but it's not free. Comparing props also takes time,
- 21:58Speaker 2: so you don't want to wrap everything in memo. Use
- 22:00Speaker 2: it strategically on components that are reasonably complex and are
- 22:04Speaker 2: likely to receive the same props, often even when the
- 22:06Speaker 2: parent rerenders.
- 22:07Speaker 1: Okay, memo for component rerenders. What about expensive calculations inside
- 22:12Speaker 1: a component.
- 22:13Speaker 2: For that, we have the use memo cook use memo
- 22:16Speaker 2: versus memo wraps a whole component. Use memo is used
- 22:19Speaker 2: inside a component to memoize the result of a specific
- 22:22Speaker 2: expensive calculation. You give use memo a function that does
- 22:25Speaker 2: the calculation and a dependency array just like use effect
- 22:28Speaker 2: ah dependency array again yep, use memo only reruns the
- 22:31Speaker 2: calculation function if one of its dependencies has changed. Otherwise
- 22:35Speaker 2: it returns the cash result from the previous render. Great
- 22:38Speaker 2: for complex computations, filtering large arrays, et cetera.
- 22:41Speaker 1: And use callback fits in here too, right, Use.
- 22:43Speaker 2: Callback, memoize as functions, use memo memois as values the
- 22:47Speaker 2: result of a function. Use callback often when passing functions
- 22:51Speaker 2: down as props to memoized components, so the prop function
- 22:55Speaker 2: itself doesn't change unnecessarily.
- 22:57Speaker 1: Okay, memo, use memo, use callback. Yeah, our optimization toolkit,
- 23:01Speaker 1: and what's.
- 23:01Speaker 2: Really exciting is the future. React nineteen is introducing an
- 23:05Speaker 2: experimental React compiler. The idea is that the compiler will
- 23:09Speaker 2: automatically analyze your code and insert memo, use memo, and
- 23:13Speaker 2: use callback where appropriate during the bill process.
- 23:16Speaker 1: WHOA, So React would optimize itself.
- 23:18Speaker 2: That's the goal. It could significantly reduce the need for
- 23:22Speaker 2: manual mammalization, which can be tricky to get right. Still experimental,
- 23:26Speaker 2: but very promising.
- 23:27Speaker 1: Very cool. Okay, that's runtime performance. What about the initial load?
- 23:30Speaker 1: Big JavaScript bundles can make that first page view slow.
- 23:33Speaker 2: Absolutely, initial load time is critical. The main technique here
- 23:36Speaker 2: is code splitting, often called lazy loading.
- 23:39Speaker 1: Lazy loading.
- 23:40Speaker 2: Yeah. Instead of bundling all your application code into one
- 23:43Speaker 2: giant JavaScript file that the user downloads up front, code
- 23:46Speaker 2: splitting lets you load parts of your code on demand,
- 23:50Speaker 2: only when they're actually needed.
- 23:51Speaker 1: How do you do that and React?
- 23:53Speaker 2: You use the React at lazy function combined with dynamic
- 23:56Speaker 2: imports synfacts. So constant my lazy component React dot lazist
- 24:00Speaker 2: my component. Then when you try to render my lazy component,
- 24:05Speaker 2: React won't have the code immediately. You need to wrap
- 24:08Speaker 2: it in a suspense component Suspense. Yes, Suspense takes a
- 24:12Speaker 2: mandatory fallback prop, which is some JSX to display while
- 24:16Speaker 2: the lazy component's code is being downloaded and loaded over
- 24:18Speaker 2: the network like suspense fallback dot loading dot div.
- 24:21Speaker 1: So the user sees a loading indicator instead of an
- 24:24Speaker 1: error or a blank space exactly.
- 24:25Speaker 2: It smoothed the experience. It reduces the initial bundle size,
- 24:29Speaker 2: making the first load faster, but adds more network requests
- 24:32Speaker 2: later when those components are.
- 24:33Speaker 1: Needed trade offs. As always, any tools for debugging performance
- 24:37Speaker 1: or other issues.
- 24:38Speaker 2: Definitely in development you should use the strict mode component.
- 24:41Speaker 2: Wrap your app in it, and React enables extra checks
- 24:44Speaker 2: and warnings for potential problems like identifying unsafe life cycle
- 24:49Speaker 2: methods or unexpected side effects. It doesn't affect the production build.
- 24:53Speaker 1: Trick mode for development checks and.
- 24:55Speaker 2: The React Developer Tools browser extension is absolutely essential. It
- 24:59Speaker 2: lets you inspect them, own a tree, view, props and state,
- 25:02Speaker 2: and critically, it has a profiler tap profiler. Yeah, you
- 25:05Speaker 2: can record interactions in your app and the profiler shows
- 25:08Speaker 2: you exactly which components rendered, why they rendered, and how
- 25:12Speaker 2: long they took invaluable for hunting down performance bottlenecks.
- 25:15Speaker 1: Reactive tools profiler.
- 25:16Speaker 2: Got it.
- 25:18Speaker 1: Okay, we've covered a ton about client side React, but
- 25:21Speaker 1: the landscape is shifting, right. How does React extend beyond
- 25:24Speaker 1: the browser into full stack territory?
- 25:26Speaker 2: Yeah, the lines are definitely blurring. Traditional client side rendering CSR,
- 25:30Speaker 2: where all the React code runs the browser has drawbacks. Well,
- 25:33Speaker 2: the initial HTML page sent from the server is often
- 25:36Speaker 2: just an empty shell with a JavaScript link. The user
- 25:39Speaker 2: sees nothing until the JS downloads, parses, and executes to
- 25:42Speaker 2: build the UI. This can feel slow and it's not
- 25:46Speaker 2: great for Search engine optimization SEO. Plus, if JS fails
- 25:50Speaker 2: or is disabled, the app is just broken.
- 25:52Speaker 1: Right. So the alternative is server.
- 25:54Speaker 2: Side rendering ASSR. With SSR, reaction components are actually rendered
- 25:58Speaker 2: into HTML on the server for the initial request. The
- 26:01Speaker 2: browser receives a fully populated HKML document.
- 26:04Speaker 1: So the user sees content immediately exactly much.
- 26:07Speaker 2: Faster perceived load time better for SEO. Then client side
- 26:10Speaker 2: jobscript hydrates the static HTM, attaching event listeners and making
- 26:14Speaker 2: it interactive.
- 26:15Speaker 1: How do you typically implement SSR with React?
- 26:18Speaker 2: Frameworks like nextjs are incredibly popular for this. Nextjs is
- 26:22Speaker 2: a full stack React framework that provides SSR, static side
- 26:25Speaker 2: generation and many other features essentially out of the box.
- 26:27Speaker 1: Next js. I hear that a lot. How does it work?
- 26:30Speaker 2: He uses file based writing. You create files and folders
- 26:33Speaker 2: inside an app directory in the latest versions, and the
- 26:36Speaker 2: file paths automatically become your application routes like app about
- 26:40Speaker 2: page dot js becomes the about route simple layout dot
- 26:44Speaker 2: js files define shared UI shells for different routes, and
- 26:48Speaker 2: you use a special link component from next link for
- 26:51Speaker 2: client side navigation between pages, which avoids full page reloads.
- 26:54Speaker 1: Okay, but if components are rendering on the server. Now,
- 26:58Speaker 1: what about components that need browsers or use hooks like
- 27:01Speaker 1: use state or use.
- 27:02Speaker 2: Effect ah excellent point. This is where a key next
- 27:05Speaker 2: dot chs and modern React concept comes in the use
- 27:08Speaker 2: client directive.
- 27:09Speaker 1: Use client by default.
- 27:11Speaker 2: Components in nextjs is app router are server components. We'll
- 27:14Speaker 2: get to those. If a component needs client side interactivity,
- 27:17Speaker 2: uses hooks, event handlers. Browser APIs. You must put the
- 27:20Speaker 2: string useed client at the very top of the file so.
- 27:22Speaker 1: That tells next dot js render this one in the browser.
- 27:25Speaker 2: Precisely it marks that component and its entire subtree as
- 27:29Speaker 2: client side components. Their code will be included in the
- 27:32Speaker 2: client JavaScript bundle. The goal is generally to push client
- 27:35Speaker 2: components as far down the tree as possible, keeping server
- 27:38Speaker 2: rendering for as much of the UI as you can,
- 27:41Speaker 2: to minimize the client bundle size.
- 27:44Speaker 1: Minimize client components. Got it? Okay, you mentioned server components,
- 27:48Speaker 1: and I've heard about server actions too. These sound like
- 27:51Speaker 1: the really cutting edge stuff. What's the deal?
- 27:53Speaker 2: They really are a major shift. React Server components rscs
- 27:59Speaker 2: are a new type of compon it designed to run
- 28:00Speaker 2: only on the server.
- 28:01Speaker 1: Only on the server, even after the initial load.
- 28:04Speaker 2: Yes, they can be rendered on the server, fetch data
- 28:07Speaker 2: directly on the server, even accessing databases or file systems,
- 28:11Speaker 2: and then their rendered output, which isn't interactive React components
- 28:14Speaker 2: more like a description of the UI is streamed to
- 28:17Speaker 2: the client.
- 28:17Speaker 1: What's the big advantage?
- 28:19Speaker 2: Their code never ships to the browser, zero impact on
- 28:21Speaker 2: your client bundle size. This makes apps potentially much faster
- 28:25Speaker 2: and lighter. Plus direct back end access simplifies data fetching drastically.
- 28:29Speaker 1: But they can't use state or effects right since their
- 28:33Speaker 1: server only.
- 28:33Speaker 2: Correct no use state, use effects, no browser APIs. They
- 28:37Speaker 2: are purely for rendering UI based on server side data
- 28:41Speaker 2: or logic. Interactivity comes from client components. Use client nested
- 28:46Speaker 2: within or alongside them.
- 28:48Speaker 1: Okay, rscs for server only rendering and data fetching. What
- 28:52Speaker 1: about changing data on the server form submissions?
- 28:56Speaker 2: That's where server actions come in. These are functions you
- 28:58Speaker 2: can define that are guarantee to run only on the server,
- 29:01Speaker 2: even if triggered from a client component.
- 29:03Speaker 1: How do you define them?
- 29:04Speaker 2: You write an ACYNC function and put the directive use
- 29:07Speaker 2: server at the very top of the function.
- 29:09Speaker 1: Body use server inside the function.
- 29:12Speaker 2: Yes, This marks it as a server action. You can
- 29:14Speaker 2: then pass this function down as a prop, maybe to
- 29:17Speaker 2: the action prop of a form, or call it from
- 29:20Speaker 2: an event handler and a client component. When invoked, the
- 29:22Speaker 2: function executes securely on the server.
- 29:24Speaker 1: So you can update a database directly from a server
- 29:26Speaker 1: action triggered by a button click.
- 29:28Speaker 2: Exactly without manually setting up API m points for simple
- 29:31Speaker 2: mutations React handles as a secure communication. You can use
- 29:35Speaker 2: hooks like use action state new and React nineteen in
- 29:39Speaker 2: client components to easily handle pending states and displieve validation
- 29:44Speaker 2: error's return from the server action.
- 29:45Speaker 1: Wow. Rsees and server actions really do blur the lines.
- 29:50Speaker 1: But what about more traditional client side routing for complex
- 29:53Speaker 1: single page apps spas? Does that still have a place.
- 29:56Speaker 2: Absolutely, While frameworks like next jas handle routing if you're
- 30:00Speaker 2: building a pure client side SPA or need more granular control,
- 30:04Speaker 2: React router is still the go to library React router.
- 30:07Speaker 1: How does it handle data loading? Now?
- 30:09Speaker 2: It's evolved a lot. Modern React Router heavily promotes using
- 30:12Speaker 2: loader functions associated with your routes loaders. Yeah, you define
- 30:15Speaker 2: a loader function for a route. React router calls this
- 30:17Speaker 2: function before your component even renders. It can fetch data,
- 30:20Speaker 2: and then the component accesses that data using the use
- 30:23Speaker 2: loader data so.
- 30:24Speaker 1: The data is ready before render. No loading spinners in
- 30:27Speaker 1: the component.
- 30:28Speaker 2: Often Yes, it simplifies component logic because the data is
- 30:33Speaker 2: just there, no need to manage loading and error states
- 30:36Speaker 2: for that initial fetch within the component itself.
- 30:39Speaker 1: What about submitting data?
- 30:40Speaker 2: React rider has action functions too similar In concept of
- 30:43Speaker 2: server actions but running client side or potentially p posting
- 30:47Speaker 2: to an endpoint. You usually trigger these using React router's
- 30:51Speaker 2: own form component, which prevents default browser navigation. Actions can
- 30:56Speaker 2: return data or trigger redirects using redirect.
- 30:58Speaker 1: And error handling.
- 30:59Speaker 2: It has robust error handling. You can define an air
- 31:02Speaker 2: element for routes and use the use root error hook
- 31:05Speaker 2: inside that element to display meaningful air information.
- 31:08Speaker 1: So React router provides data loading, submission, and airror handling
- 31:12Speaker 1: tied to Ritz exactly.
- 31:13Speaker 2: And you can combine its features with newer React capabilities too.
- 31:16Speaker 2: For example, you might use reacts use optimistic hook within
- 31:19Speaker 2: a component to show immediate UI feedback while iraq router
- 31:23Speaker 2: action runs in the background, or use React routers de
- 31:25Speaker 2: fer utility and a weight component to stream data that
- 31:28Speaker 2: might be slow rendering fallback UI while waiting, improving perceived performance.
- 31:33Speaker 1: Okay, lots of powerful tools. Combining there few, we've covered
- 31:36Speaker 1: a massive amount of ground. We went from the basic
- 31:39Speaker 1: problem React solves escaping imperative dominipulation all the way through
- 31:43Speaker 1: components prop state effects, optimization techniques like memmorization and lazy loading,
- 31:48Speaker 1: and then into the full stack world with SSR next JS, React,
- 31:51Speaker 1: server components, server actions, and advance client side routing with
- 31:55Speaker 1: React router. If we try and connect this all to
- 31:58Speaker 1: the bigger picture. For some when listening, they now have
- 32:01Speaker 1: this incredible toolkit conceptually and practically to build really interactive,
- 32:06Speaker 1: really performant web applications. But this raises a question for me.
- 32:10Speaker 1: Looking at things like server components, server actions, the tight
- 32:13Speaker 1: integration and frameworks like next JS, where do you see
- 32:16Speaker 1: that line between front end and back end development blurring
- 32:19Speaker 1: the most for React developers in the next few years.
- 32:21Speaker 1: What's the biggest shift you see coming from all this.
- 32:23Speaker 2: That's a great question. I think the biggest shift is
- 32:26Speaker 2: that the React developer is increasingly becoming a product engineer
- 32:30Speaker 2: who needs to be comfortable thinking across the entire stack,
- 32:33Speaker 2: from UI interactions right down to data fetching and mutations
- 32:37Speaker 2: on the server, server components and actions especially pull back
- 32:41Speaker 2: end concerns much closer to the UI code. The lines
- 32:44Speaker 2: are blurring significantly. You're writing server logic right alongside your
- 32:48Speaker 2: component logic. While you still need specialists. The average React
- 32:51Speaker 2: developer will likely need a broader understanding than before, but
- 32:55Speaker 2: the payoff is potentially much faster development cycles. And more
- 32:58Speaker 2: seamless features. But at the end of the day, my best
- 33:00Speaker 2: advice for anyone listening is simply practice is key. Theory
- 33:05Speaker 2: is great, but you need to build things.
- 33:07Speaker 1: Absolutely.
- 33:07Speaker 2: You should definitely try building small demo projects. Maybe a
- 33:10Speaker 2: shopping cart app that hits state lists, maybe some effects.
- 33:14Speaker 2: An authentication system great for forms state maybe context or
- 33:18Speaker 2: custom books. Even an event management website could involve routing,
- 33:21Speaker 2: data fetching, maybe server components. If you use NEXTJS.
- 33:24Speaker 1: Good project ideas.
- 33:26Speaker 2: And as you build, remember you don't have to reinvent
- 33:29Speaker 2: the wheel. Explore the ecosystem. Libraries like tan stack Query
- 33:34Speaker 2: or React query are fantastic for complex data fetching, caching
- 33:38Speaker 2: and synchronization, Simmer Motion for animations, react to form or
- 33:42Speaker 2: forma for complex forms, and for global state management. Beyond
- 33:45Speaker 2: simple prop drilling or context libraries like redoct zoos, standard
- 33:49Speaker 2: Joe tie are popular choices. Leverage these tools.
- 33:52Speaker 1: Fantastic advice, build things, explore the ecosystem. Well, thank you
- 33:55Speaker 1: for joining us on this really deep dive into React.
- 33:57Speaker 1: For everyone listening, keep building, keep exploring, and keep learning.