Experimental
Optional
options: { Optional
Else?: ComponentType<any>const AdminDashboard: React.FC = () => {
return <>...</>
}
const SafeAdminDashboard = withIf<"isAdmin", boolean>("isAdmin")(AdminDashboard)
// renders <AdminDashboard />
<SafeAdminDashboard isAdmin={true} />
// renders null
<SafeAdminDashboard isAdmin={false} />
const HappyScreen: React.FC = () => {
return <>...</>
}
const BlueErrorScreen: React.FC = () => {
return <>...</>
}
const NewComponent = withIf("error", {
Else: BlueErrorScreen
})(HappyScreen)
// renders <HappyCase />
<NewComponent error={null} />
// renders <BlueErrorScreen />
<NewComponent error={new Error("some error")} />
type HappinessLevel = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
const HappyScreen: React.FC = () => {
return <>...</>
}
const SadScreen: React.FC = () => {
return <>...</>
}
const MoodScreen = withIf<{ happinessLevel: HappinessLevel }>(
({ happinessLevel }) => happinessLevel >= 5,
{
dependencyNames: ["happinessLevel"],
Else: SadScreen,
},
)(HappyScreen);
// renders <HappyScreen />
<MoodScreen happinessLevel={10} />
// renders <SadScreen />
<MoodScreen happinessLevel={3} />
Experimental
renders conditionally
It could have a better name
Optional
Else?: ComponentType<any>const AdminDashboard: React.FC = () => {
return <>...</>
}
const SafeAdminDashboard = withIf<"isAdmin", boolean>("isAdmin")(AdminDashboard)
// renders <AdminDashboard />
<SafeAdminDashboard isAdmin={true} />
// renders null
<SafeAdminDashboard isAdmin={false} />
const HappyScreen: React.FC = () => {
return <>...</>
}
const BlueErrorScreen: React.FC = () => {
return <>...</>
}
const NewComponent = withIf("error", {
Else: BlueErrorScreen
})(HappyScreen)
// renders <HappyCase />
<NewComponent error={null} />
// renders <BlueErrorScreen />
<NewComponent error={new Error("some error")} />
type HappinessLevel = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
const HappyScreen: React.FC = () => {
return <>...</>
}
const SadScreen: React.FC = () => {
return <>...</>
}
const MoodScreen = withIf<{ happinessLevel: HappinessLevel }>(
({ happinessLevel }) => happinessLevel >= 5,
{
dependencyNames: ["happinessLevel"],
Else: SadScreen,
},
)(HappyScreen);
// renders <HappyScreen />
<MoodScreen happinessLevel={10} />
// renders <SadScreen />
<MoodScreen happinessLevel={3} />
Generated using TypeDoc
renders conditionally
It could have a better name