When working with values that could be null or undefined, implement defensive coding patterns to prevent runtime errors. This practice is essential for robust React applications.
When working with values that could be null or undefined, implement defensive coding patterns to prevent runtime errors. This practice is essential for robust React applications.
Three key techniques to apply:
!!
) to safely convert to boolean:
```js
// Risky - may cause unexpected behavior if selected is undefined
props: ({ selected }) => selected,// Safe - explicitly converts to boolean props: ({ selected }) => !!selected,
2. **Apply optional chaining (`?.`) for property access and method calls:**
```js
// Risky - will throw if child or props is null/undefined
child.props.onKeyDown(event);
// Safe - gracefully handles null/undefined at any level
child?.props?.onKeyDown?.(event);
${title} - ${productName}
// Safe - handles undefined case explicitly
${title}${productName ?
- ${productName} : ''}
```
These patterns create more resilient code by anticipating and gracefully handling potential null or undefined values throughout your application.
Enter the URL of a public GitHub repository