Conditional Rendering over Nil Coalescing
The article emphasizes the importance of avoiding nil coalescing in UI code. Instead of using fallback values, developers should implement conditional rendering to handle nil values appropriately. This approach enhances user experience by providing clear indicators for loading states, errors, or empty content.
- ▪Nil coalescing can introduce significant issues in UI code.
- ▪Conditional rendering is recommended to handle nil values effectively.
- ▪Implementing loading indicators and error messages improves user experience.
Opening excerpt (first ~120 words) tap to expand
Stop writing count ?? 0 in your UI code. Stop writing items ?? [] in your API return values. Please, for the love of god, stop writing item?.id ?? "", especially. Coalescing feels satisfying when you appease the compiler or typechecker, but what you're doing is introducing a ticking timebomb instead. I've been guilty of using each of these patterns over my career. What I should have done instead was understand why those values could be nil and handle them with some loud error or better yet, some UI that indicated these values were missing or loading or just plain empty still. I see so much UI code that defaults to these fallback values, rather than conditionally rendering a loading indicator, an error message or an empty state.
…
Excerpt limited to ~120 words for fair-use compliance. The full article is at Blog - Eli Perkins.