Reactiflux
Reactiflux

help-js

Root Question Message

⛄ Snowberb ⛄
⛄ Snowberb ⛄11/23/2022
Will this piece of code crash the app if it doesnt find anything?
const { fullName } = clients.find((client) => client.clientId === customerId);
Message Not Public

Sign In and Join Server To See

11/23/2022
Message Not Public

Sign In and Join Server To See

11/23/2022
ScriptyChris
ScriptyChris11/23/2022
@191883133430398976 add a default value if .find() returns null:
const { fullName } = clients.find((client) => client.clientId === customerId) ?? { fullName: '' };
⛄ Snowberb ⛄
⛄ Snowberb ⛄11/23/2022
ooo I like this
⛄ Snowberb ⛄
⛄ Snowberb ⛄11/23/2022
that's what I wanted
⛄ Snowberb ⛄
⛄ Snowberb ⛄11/23/2022
but why ?? and not ||?
Message Not Public

Sign In and Join Server To See

11/23/2022
ScriptyChris
ScriptyChris11/23/2022
Alternatively you could use optional chaining without spread 🤔
const fullName = clients.find((client) => client.clientId === customerId)?.fullName;
ScriptyChris
ScriptyChris11/23/2022
!mdn optional chaining
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy