Leerob builds a basic list with a React class component, then rewrites it as a function component that renders the same JSX. He recommends function components for simple cases because they require less code and avoid class-specific `this` syntax.
React Overview
Leerob, a developer teaching the Learn Next.js course, gives a React overview with examples from a simple Next.js application.
Props let a component receive values such as an array of list items, so the same list can be reused with different content. Leerob maps over the items and adds a `key` to each rendered list item. Destructuring extracts `items` from props, while rest and spread pass remaining props such as `aria-label` to the unordered list.
Leerob adds a search input that filters the list as the user types. The class component initializes filtered items from its props, binds its filter method, copies the original array, and updates state with matching items. The example uses `filter` and checks whether each item starts with the search value.
Leerob rewrites the filter as a function component using `React.useState`, with `filteredItems` and `setFilteredItems` replacing class state and `this.setState`. The hook version removes the constructor and method binding while keeping the same filtering behavior.
Takeaways
- Use function components for simple UI that only needs to render data.
- Pass changing content through props to make components reusable.
- Add a `key` when rendering lists with `map`.
- Copy arrays before filtering so the original props are not modified.
- Use `useState` to manage local state in function components.
Mentioned in this talk
Search inside this talk
What was claimed 2
- featureReact: Hooks let functional components access state and React lifecycle-related behavior.
“Hooks let us get away from class-based components and use functional components instead.”
YouTube Unverified1 source· Talk
- featureReact: UseState provides a state value and a function to update it.
“here’s the value of state, and here’s a function that’s going to allow you to mutate that state.”
YouTube Unverified1 source· Talk
Related talks
23:33
6:35
26:31
14:29
12:33
3:05