Reputation: 319
I have this in my jsx
{get(applicant, 'user.name').map((obj,i) => <span>{obj}</span>}
if user.name not exist, my app will crash, why get doesn't solve the problem here? It's lodash's get.
If I do applicant.user.name.map my app will have chance to crash.
Upvotes: 2
Views: 436
Reputation: 8509
Lodash _.get return undefined
if path doesn't exist. As result undefined.map
throws the error. You should pass []
as the third argument, in that case, if path is not exist _.get
returns []
and [].map
does not throws the error:
{get(applicant, 'user.name', []).map((obj,i) => <span>{obj}</span>}
Upvotes: 1