S
SolidJS11mo ago
versa

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 🐍11mo ago
Do you really need memo here?
ai6
ai611mo 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>
</>
);