My React app crashed right in front of my mentor Turns out the problem was simple - the API data hadn’t loaded yet. I tried accessing nested data immediately and got the classic error: Before: user.profile.name Cannot read properties of undefined. App dead. After: user?.profile?.name After staring at the code for a bit, I realized the fix was just using optional chaining (?.) That small change made React check if the data exists before accessing it and the crash was gone. Funny how one tiny character can save your project Lesson I learned today: When working with API data in React, always assume the data might not be there yet. Sometimes the smallest fixes teach the biggest lessons. #ReactJS #MERNStack #JavaScript #WebDevelopment
Put everywhere chaining operator is questionable solution, because this approach causes bad DX, better solution would be declare some kind of contract, validate server response using modern validation libraries like Zod and gracefully handle validation error
The thing you showed is actually really bad idea. I mean in the terms of syntax it's fine, but frontend and basically presentation layer should not rely (if possible) on these nested props. Here is what should happen: 1. When data is fetched is mapped to flat view model structure, then if there is no user, just do not provide it's value or set as null. Then all differences between expected contract from backend are catchable with proper strategy or fallback in single place.
This example of use is not the best. In fact, in your case, display "User Name" should not be solution. In case of "user is falsy", you should display something else and without any "user" in the process. The solution is not optional chaining but somethink like a "if" in the upper caller. I think it is important to take care of exemple cause naïve programmer will : * propage wrong solution of a problem. * evict bug but not solve it. Anyway, optional chainning is very useful in some specifics cases but not to evict bugs.
Not bad. But here 2 things that you can do to avoid these situations. As you’ve already been advised, having a loading and error states, one of the best way to solve this (and other issues you will eventually encounter) is react-query from tanstack. The second would be to migrate your project to typescript. This wouldn’t have happened, even leaving aside the additional states when fetching. Solved on ts easily, by adding before that return a: !user?.profile?.name return null
optional chaining is not bad but what i think is the better way here is to ensure proper error handling might be using try...catch, so the component receive validated data which can sometime reduce the need for deep optional checks. Also, if still want to go with optional chaining, nullish coalescing (??) might make it even clearer for fallback value (null, undefine).
Great example of a very real-world React problem 👍 Optional chaining definitely saves you from runtime crashes, especially when dealing with async API data. But what this really highlights is the importance of designing UI around loading and data states, not just preventing errors. In production apps, I usually think in terms of three states: loading (data hasn’t arrived yet) success (data is available) error (request failed) Optional chaining is a solid safety net, but combining it with proper conditional rendering, loading placeholders, or data-fetching tools like React Query/SWR makes components much more predictable and easier to maintain. Every React developer hits this moment at least once — and it’s often the point where you start thinking more about data flow and state timing rather than just syntax. Small fix, big learning indeed.
Assuming name is always available (ideal scenario though we wait for data to be available after network call), why can't we have null as initial state and just check user && user.bl.ah.ah. 😄😛
Glad you got to know about it in a right way. But overuse of this is also bad.
Optional chaining might be a hack but doesn't fit every where In case of an API call we have 3 cases in any scenario loading, data and error The loading is always top check then comes data if doesn't exist early return then comes the last case where a specific property is missing that too could result into an undefined so there must be a fallback