React Data Grid Component: MUI X DataGrid and the Alternatives

React data grid component: the MUI X DataGrid rendering four order rows with sortable columns and pagination

A data grid is a table that sorts, filters, paginates and selects rows for you. In React the usual choice is MUI X DataGrid, whose Community version is free and MIT licensed, and this guide builds a working grid with it: columns, pagination, sorting, checkbox selection, formatted and custom cells. It also answers the question … Read more >>

React Error Boundaries with Functional Components

React error boundary with functional components: the browser showing a fallback message while the rest of the dashboard keeps working

To use a React error boundary with functional components, you still need one class: React has no hook that catches rendering errors, so the boundary itself has to be a class component even in an app made entirely of function components. You write that class once and wrap your function components in it. The usual … Read more >>

Merge Arrays of Objects in TypeScript (concat, spread, by id)

Merge arrays of objects in TypeScript: Command Prompt output of two order arrays combined with concat and spread

To merge two arrays in TypeScript, use concat() or spread syntax: a.concat(b) and […a, …b] both return a new array and leave the originals alone. That is all you need when the rows are independent. When both arrays hold objects that describe the same records, you usually want one row per id instead of two, … Read more >>

How to Initialize an Array in TypeScript

Initialize an array in TypeScript: Command Prompt output of typed arrays declared with values, empty and with the generic Array syntax

To initialize an array in TypeScript, declare the element type and give it a value: const cities: string[] = [“Austin”, “Denver”], or const ids: string[] = [] to start empty. Array<string> means the same as string[]. For a fixed number of slots use Array.from({ length: n }, …) rather than new Array(n), which leaves holes. … Read more >>

Check if an Array Is Null or Empty in TypeScript

Check if an array is null or empty in TypeScript: Command Prompt output showing the check against a filled array, an empty array, null and undefined

To check if an array is null or empty in TypeScript, write if (!items || items.length === 0), or the shorter if (!items?.length), which covers null, undefined and [] in one test. The order matters: reading .length before the null check throws at runtime. This guide shows both forms running against all four cases, the … Read more >>

Find the Maximum Value in an Array of Objects in TypeScript

Find the maximum value in an array of objects in TypeScript: Command Prompt output of the highest salary and the employee who earns it

To find the maximum value in an array of objects in TypeScript, decide first what you want back. For just the number, map the property and use Math.max(): Math.max(…employees.map(e => e.salary)). For the whole object that holds it, use reduce() and keep whichever item is larger. Both are one line, and both have an edge … Read more >>

Filter an Array of Objects by Multiple Properties in TypeScript

Filter an array of objects by multiple properties in TypeScript: Command Prompt output of AND, OR and mixed conditions on orders

To filter an array of objects by multiple properties in TypeScript, combine the tests inside one predicate: orders.filter(o => o.status === “shipped” && o.city === “Austin”) for “all of these”, and || for “any of these”. That covers two fixed conditions. Real screens need more: several selected values for the same field, ranges, a search … Read more >>

React: Cannot Update a Component While Rendering a Different Component

React warning cannot update a component while rendering a different component, shown in the browser with the real console text

React prints this warning when one component changes another component’s state while it is rendering. Depending on your React version the text is either “Cannot update a component (Parent) while rendering a different component (Child)” or the older “Cannot update a component from inside the function body of a different component”. Both mean the same … Read more >>

React Hooks in Class Components: What Works Instead

React hooks in class components: the browser showing the error React throws when useState is called inside a class render

You cannot use React hooks inside a class component. Hooks only work in function components and in custom hooks, and React throws an error if you call one from a class. There are three practical ways round it: wrap the class in a small function component that calls the hook and passes the values down … Read more >>