DEV Community

Discussion on: A React "if component"

Collapse
 
shadowtime2000 profile image
shadowtime2000
import { memo } from "react";

function If({ children, condition }) {
    return condition ? children : null;
}

export default memo(If, (props, oldProps) => props.condition === oldProps.condition);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
koire profile image
Koire

This is a really good expression of it.
I've been trying to do something very similar since we have something like

<Title> {newAccount ? "newtitle": "oldtitle"}</Title>
Enter fullscreen mode Exit fullscreen mode

but we have a few ways for newAccount to be true
something like

<IF condition={isNewAccount}> This is a new account</IF>
Enter fullscreen mode Exit fullscreen mode

is a start