S
SolidJS16mo ago
tourist

idiomatic memo usage

is there a more idiomatic way to write this?
const username = createMemo(() => state.data?.displayName?.toLowerCase().split(" ")[0]);

return (
<>
<p>welcome{username() ? `, ${username()}` : ""}</p>
const username = createMemo(() => state.data?.displayName?.toLowerCase().split(" ")[0]);

return (
<>
<p>welcome{username() ? `, ${username()}` : ""}</p>
2 Replies
foxpro 🐍
foxpro 🐍16mo ago
Do you really need memo here?
ai6
ai616mo ago
Not sure what you mean by "more idiomatic" but you could make it easier to read by extracting it to a function:
const getWelcomeMessage = () => {
const username = state.data?.displayName?.toLowerCase().split(" ")[0];
return `welcome${username ? `, ${username}` : ""}`;
};

return (
<>
<p>{getWelcomeMessage()}</p>
</>
);
const getWelcomeMessage = () => {
const username = state.data?.displayName?.toLowerCase().split(" ")[0];
return `welcome${username ? `, ${username}` : ""}`;
};

return (
<>
<p>{getWelcomeMessage()}</p>
</>
);
Want results from more Discord servers?
Add your server