?✨ In modern JavaScript and React applications, it's common to encounter runtime errors caused by accessing properties on undefined or null values, or calling methods on undefined arrays or objects. These issues can disrupt user experience and make debugging a nightmare. In this article, we'll identify the common issues and provide an ESLint configuration to mitigate them effectively. ??
?✨ In our React or React Native projects, because we are not using TypeScript, we sometimes forget to write safe code. These unsafe codes can lead to many issues in production, like crashing React Native apps, frustrating users, and complicating maintenance. Let's dive into these common issues and how to solve them. ???
Accessing a property on an object that is undefined or null causes a runtime error:
const user = undefined; console.log(user.name); // ❌ Runtime Error: Cannot read property 'name' of undefined
const name = user?.name;
const name = user ? user.name : 'Default Name';
const user = { name: 'Default Name' };
Calling methods like .map() or .filter() on an undefined array throws an error:
const items = undefined; items.map((item) => console.log(item)); // ❌ Runtime Error: Cannot read property 'map' of undefined
if (Array.isArray(items)) { items.map(item => console.log(item)); }
const items = someValue || []; items.map(item => console.log(item));
const items = possibleItems ?? [];
Trying to call a function that might be undefined:
const handler = undefined; handler(); // ❌ Runtime Error: handler is not a function
if (typeof handler === 'function') { handler(); }
const handler = passedHandler || (() => {});
Destructuring properties from an undefined object results in an error:
const user = undefined; console.log(user.name); // ❌ Runtime Error: Cannot read property 'name' of undefined
const name = user?.name;
const name = user ? user.name : 'Default Name';
Accessing elements of an undefined array causes an error:
const user = { name: 'Default Name' };
const items = undefined; items.map((item) => console.log(item)); // ❌ Runtime Error: Cannot read property 'map' of undefined
if (Array.isArray(items)) { items.map(item => console.log(item)); }
Using array methods like .map() or .filter() on undefined values or objects:
const items = someValue || []; items.map(item => console.log(item));
const items = possibleItems ?? [];
const handler = undefined; handler(); // ❌ Runtime Error: handler is not a function
Failing to validate conditions strictly can lead to bugs, such as relying on falsy values. For example, if conditions expecting a boolean might incorrectly evaluate other types like undefined or 0:
if (typeof handler === 'function') { handler(); }
const handler = passedHandler || (() => {});
const obj = undefined; const { name } = obj; // ❌ Runtime Error: Cannot destructure property 'name' of undefined
const { name = 'Default Name' } = user || {};
To catch these issues during development, we can leverage ESLint with specific rules. Below is an ESLint configuration that will flag unsafe calls and suggest fixes. ?️??
Add the following rules to your .eslintrc.js or ESLint configuration file:
const user = undefined; console.log(user.name); // ❌ Runtime Error: Cannot read property 'name' of undefined
To enable these rules, ensure you have the necessary ESLint plugins and parsers installed:
const name = user?.name;
Add the following to your settings.json:
const name = user ? user.name : 'Default Name';
Add an npm script to run ESLint:
const user = { name: 'Default Name' };
Then, run npm run lint to catch issues. ?
By implementing the above ESLint rules and practices, you can catch and fix unsafe calls before they become runtime errors. ?? This approach will improve the reliability and maintainability of your JavaScript and React projects. ?⚙️✨
The above is the detailed content of Avoiding Unsafe Calls in JavaScript and React Projects with ESLint. For more information, please follow other related articles on the PHP Chinese website!