Package Exports
- components-switch
- components-switch/package.json
Readme
π components-switch
A lightweight React utility component library featuring
Switch
andMatch
for elegant conditional renderingβinspired by SolidJS, made for React.
β¨ Features
- π§ Declarative conditional rendering
- π‘ Inspired by SolidJS's
Switch/Match
logic - π© Tiny footprint & zero dependencies
- βοΈ Fully typed and compatible with React 17/18+
π¦ Installation
npm install components-switch
Or with yarn:
yarn add react-switch-match
π§± Usage
import { Switch, Match } from 'react-switch-match';
function Dashboard() {
return (
<Switch fallback={<p>If nothing is true, show this</p>}>
<Match when={shouldShowA}>
<Table />
</Match>
<Match when={shouldShowB() === true}>
<Analytics />
</Match>
<Match when={true}>
<SomethingElse />
</Match>
</Switch>
);
}
import { Switch, Match } from 'react-switch-match';
function Greeting({ isLoggedIn }: { isLoggedIn: boolean }) {
return (
<Switch fallback={<p>Please log in.</p>}>
<Match when={isLoggedIn}>
<p>Welcome back!</p>
</Match>
</Switch>
);
}