React Hooks: Revisited
course
In this collection, we'll go through each of the standard hooks offered by the React library, learn how they work and build small applications that leverage them internally.
Browse
Find courses, lessons, articles, talks, and podcasts.
40 resources
Showing 25–40 · Page 2 of 2
course
In this collection, we'll go through each of the standard hooks offered by the React library, learn how they work and build small applications that leverage them internally.
lessonDuration 2m 1sFree
In some cases, you may have multiple GitHub accounts that you need to interact with from the same system. For example, you could have an account for work and another for personal projects, but you don't want to re-authenticate each time you switch between then. By using public SSH keys for each account, you can eliminate this part of the process. In this lesson, we will create two SSH keys in our terminal using `ssh-keygen` , copy them to our clipboard using `pbcopy` and then add them to our respective GitHub accounts. Commands we'll be using include: - `ssh-keygen` - `ls` - `pbcopy`
lessonDuration 3m 54sFree
When working with multiple GitHub accounts on the same system, you want to ensure that your commits have the proper author information. To do this, we'll create a global `git` configuration on our system that uses our personal account's information. Then, in our work-specific repositories, we'll add a local `git` configuration to override our global values. We will be using the following commands in this lesson: - `code` - `git config`
lessonDuration 2m 35sFree
It can be difficult to work on multiple projects that use different versions of Node. `nvm` ([Node Version Manger](https://github.com/nvm-sh/nvm)) make this easier by allowing you to download multiple versions and switch between them. You can also add a NVM configuration file (i.e. `.nvmrc`) to specify which version you want to use for each project. In this lesson, we'll cover the following commands: - `nvm install` - `nvm ls` - `nvm ls-remote` - `nvm use`
lessonDuration 3m 26sFree
In this lesson, we have two local SSH keys: one associated with our personal GitHub account, the other with our work account. To more easily manage them, we will use `ssh-add` to add our keys to our system's key manager `ssh-agent`. Then, we will create a SSH configuration file, which will determine what key to use when connecting to GitHub based upon the URL. Once we have set up our configuration file, we can test our connections using `ssh -T` and clone repositories with `git clone`. Commands we will be using in the lesson: - `ls` - `ssh-add` - `touch` - `ssh` - `git clone`
lessonDuration 4m 10sFree
Using the [GitHub CLI](https://github.com/cli/cli) and a shell script, we will automate the process of monitoring pull requests in repositories we maintain. Specifically, open PRs we have created and those awaiting our review.
lessonDuration 3m 40s
The `useMemo` hook is a performance optimization in React which leverages "memoization" under the hood. Memoization is essentially an internal caching mechanism that allows functions to avoid re-computing expensive values when receiving the same inputs. `useMemo` has the same function signature as `useEffect` and `useLayoutEffect` because it also takes a function and optional dependency array as its arguments. Unlike those hooks, however, the value returned by `useMemo` is a memoized value. **Note:** The React documentation suggests that you implement your logic with `useEffect` first to make sure your application works. Then, you can go back and refactor with `useMemo` to improve app performance. ```jsx // Hook runs whenever the value of 'a' or 'b' change const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]) // Receiving input for the first time returns a newly computed value computeExpensiveValue(1, 2) // Receiving the same input arguments returns a cached value computeExpensiveValue
lessonDuration 1m 57s
The `useRef` hook allows us to leverage React refs within our function components. Refs can be assigned to DOM elements in order to allow us to manipulate them directly. It is important, however, to note that this is somewhat of an anti-pattern in React as JavaScript frameworks usually handle updating the browser DOM after the virtual DOM has changed. `useRef` only takes one argument: its initial value. In the code snippet below, you can see how it is then assigned to an `<input />`: ```jsx // Instantiate the ref const inputRef = useRef(null); // Assign `inputRef` to the <input /> element <input ref={inputRef} type="search" /> ```
lessonDuration 2m 14s
The `useDebugValue` hook will not effect your user experience but is instead aimed at improving the developer experience. When building your own custom hooks, `useDebugValue` can help you debug the hook's internal logic by displaying values in the React Dev Tools. The hook takes two arguments: a value to be displayed in the developer console and an optional function you can use to format the aforementioned value. The function signature looks like this: ```jsx const name = 'ryan' // Outputs 'ryan' to the developer console useDebugValue(name); // Outputs 'RYAN' to the developer console useDebugValue(name, (name) => name.toUpperCase()); ```
lessonDuration 2m 5s
Similar to `useMemo`, the `useCallback` hook also uses memoization internally in order to improve application performance. The difference is the type of value the two hooks return: while `useMemo` returns a memoized value, `useCallback` returns a memoized function. This allows you to create memoized functions and pass them as props between components without having to repeatedly define the caching mechanism in order to avoid expensive, repetitive computations: ```jsx // Returns a memoized function const memoizedFunction = useCallback(() => { // Define function logic }, [data]) ```
lessonDuration 4m 14s
The `useEffect` hook is used to handle side-effects caused by a component's actions such as fetching data from an API, attaching event listeners to the document or manipulating the DOM itself. `useEffect` takes two values: a function defining the logic to be executed and an optional array called the "dependency array". By default, effect hooks will run every time the component re-renders, however, you can control its execution by defining and changing the values within your dependency array. ```jsx useEffect(() => { // Runs every time the component renders }) useEffect(() => { // Runs only on initial render }, []) // Optional second argument: dependency array useEffect(() => { // Runs only when 'apiData' changes }, [apiData]) ```
lessonDuration 4m 56s
Normally, you would use `useState` to store values in your function components. However, when your state value is a complex data object or is comprised of a number of related values, you may want to use `useReducer` instead. This hook allows you to avoid having numerous state hooks and, instead, consolidate your values. Like `useState`, the array returned by the hook contains two values: your current state and a dispatch function which allows you to update the state. When instantiating the hook, you'll need to pass it two arguments, a reducer function and your initial state. Similar to patterns used in libraries like Redux, when you dispatch an action the reducer function body will determine how to update the internal state. This is the function signature of the `useReducer` hook: ```jsx const [state, dispatch] = useReducer(reducer, initialState) ```
lessonDuration 2m 26s
At first glance, the `useLayoutEffect` and `useEffect` hooks look the same because they have a very similar function signature. There is, however, a subtle but important difference between them: the timing of when they get executed. While `useEffect` runs **after** the DOM paints to avoid blocking the page load, `useLayoutEffect` runs **before** the DOM paints, which can help avoid issues with positioning or styling. Generally speaking, you want to use the `useLayoutEffect` instead of `useEffect` when you are interacting with the DOM directly. Like `useEffect`, `useLayoutEffect` takes two arguments: a function and an optional dependency array. ```jsx useLayoutEffect(() => { // Runs every time the component renders }, []) useLayoutEffect(() => { // Runs only on initial render }, []) // Optional second argument: dependency array useLayoutEffect(() => { // Runs only when 'apiData' changes }, [apiData]) ```
lessonDuration 3m 10s
While React refs are primarily used to interact with DOM elements, `useRef` can also be used to store values in your function components. Unlike `useState`, when this value changes the component will **not** re-render, which means you can leverage `useRef` to avoid unnecessary renders and optimize your application's performance. ```jsx // Instantiate useRef const countRef = useRef(0) // Increment the value while avoiding a re-render countRef.current = countRef.current + 1 ```
lessonDuration 4m 23s
The `useContext` hook allows you to leverage the React Context API within your function components. This lets your components provide data to their descendants in the component tree while avoiding "prop drilling" (i.e. passing props through components that are not concerned with the data). When instantiating the hook, it takes one argument: a React context. This subscribes the component to the context and any updated values being provided by the `Context.Provider`. Then, any data being provided by the parent component will be accessible in this component via the `context.current` field. ```jsx // Create a context const SearchContext = React.createContext(); // Subscribe to SearchContext const context = useContext(SearchContext); ```
course
SSH provides a secure way for your to [connect to and authenticate with GitHub's servers](https://help.github.com/en/github/authenticating-to-github/about-ssh). While HTTPS connections use usernames and passwords as credentials, SSH instead uses public and private key pairs. These can be generated on your system using a CLI like [`ssh-keygen`](https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent) and then associated with your GitHub account using the public key. In some cases, you may need to connect to more than one GitHub account from the same system. For example, you may have [separate accounts for personal and professional projects](https://ryanharris.dev/managing-ssh-keys-for-github/), which can be difficult to manage manually. Using `ssh-agent` and a SSH configuration file, you can alleviate this pain point by automatically determining which key to use based on your GitHub repository's `git` URL.