Skip to content

Commit

Permalink
vault backup: 2024-07-07 13:55:02
Browse files Browse the repository at this point in the history
  • Loading branch information
Sreesanth46 committed Jul 7, 2024
1 parent 3cf8f35 commit 73596fa
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 217 deletions.
12 changes: 6 additions & 6 deletions .obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"state": {
"type": "markdown",
"state": {
"file": "infinite-scroll.md",
"file": "vue-teleport.md",
"mode": "source",
"source": true
}
Expand Down Expand Up @@ -85,7 +85,7 @@
"state": {
"type": "backlink",
"state": {
"file": "infinite-scroll.md",
"file": "vue-teleport.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -102,7 +102,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "infinite-scroll.md",
"file": "vue-teleport.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
}
Expand All @@ -125,7 +125,7 @@
"state": {
"type": "outline",
"state": {
"file": "infinite-scroll.md"
"file": "vue-teleport.md"
}
}
}
Expand All @@ -148,11 +148,11 @@
},
"active": "7d37364f602a0421",
"lastOpenFiles": [
"context-manager-in-python.md",
"vue-render-function.md",
"user-session-management-in-microservice.md",
"vue-teleport.md",
"show-component-for-react.md",
"infinite-scroll.md",
"vue-teleport.md"
"context-manager-in-python.md"
]
}
117 changes: 60 additions & 57 deletions show-component-for-react.md
Original file line number Diff line number Diff line change
@@ -1,58 +1,61 @@
# Show component for React

As a React developer, you know that conditional rendering is a task that comes up all the time. While you can always rely on basic inline conditional logic (using && and ternary operators), things can get messy when you have multiple elements that need to render or not render.
<br>
To make conditional UI rendering simpler and more declarative, I've adopted a custom Show component for my projects. Here's how it works:
The Show component accepts React elements wrapped in a When and Else component for conditional logic.
<br>
Now, say goodbye to the verbosity of condition ? "When true" : "Else case". Embrace the elegance and simplicity of the Show component for a more streamlined and intuitive approach to conditional rendering in your React projects.
No more parsing complex ternary statements or logical operators to figure out what will actually render!
<br><br>
```ts
import React, { Children, ReactElement } from 'react';

interface WhenProps {
isTrue: boolean;
children: React.ReactNode;
}

interface ElseProps {
render?: React.ReactNode;
children: React.ReactNode;
}

export const Show = (props: React.PropsWithChildren) => {
let when: React.ReactNode | null = null;
let otherwise: React.ReactNode | null = null;

Children.forEach(props.children, child => {
if ((child as ReactElement<WhenProps>).props.isTrue === undefined) {
otherwise = child;
} else if (!when && (child as ReactElement<WhenProps>).props.isTrue) {
when = child;
}
});

return when || otherwise;
};

Show.When = ({ isTrue, children }: WhenProps) => isTrue && children;

Show.Else = ({ render, children }: ElseProps) => render || children;
```
<br><br>
```ts
export default function App() {
const condition = true; // Add your condition
return (
<div className="p-6">
<Show>
<Show.When isTrue={condition}>Renders when condition is true</Show.When>
<Show.Else>Render when condition is not true</Show.Else>
</Show>
</div>
);
}
```

---
title: Show component for React
date: 2024-03-21T01:20:00.000+00:00
---

As a React developer, you know that conditional rendering is a task that comes up all the time. While you can always rely on basic inline conditional logic (using && and ternary operators), things can get messy when you have multiple elements that need to render or not render.
<br>
To make conditional UI rendering simpler and more declarative, I've adopted a custom Show component for my projects. Here's how it works:
The Show component accepts React elements wrapped in a When and Else component for conditional logic.
<br>
Now, say goodbye to the verbosity of condition ? "When true" : "Else case". Embrace the elegance and simplicity of the Show component for a more streamlined and intuitive approach to conditional rendering in your React projects.
No more parsing complex ternary statements or logical operators to figure out what will actually render!
<br><br>
```ts
import React, { Children, ReactElement } from 'react';

interface WhenProps {
isTrue: boolean;
children: React.ReactNode;
}

interface ElseProps {
render?: React.ReactNode;
children: React.ReactNode;
}

export const Show = (props: React.PropsWithChildren) => {
let when: React.ReactNode | null = null;
let otherwise: React.ReactNode | null = null;

Children.forEach(props.children, child => {
if ((child as ReactElement<WhenProps>).props.isTrue === undefined) {
otherwise = child;
} else if (!when && (child as ReactElement<WhenProps>).props.isTrue) {
when = child;
}
});

return when || otherwise;
};

Show.When = ({ isTrue, children }: WhenProps) => isTrue && children;

Show.Else = ({ render, children }: ElseProps) => render || children;
```
<br><br>
```ts
export default function App() {
const condition = true; // Add your condition
return (
<div className="p-6">
<Show>
<Show.When isTrue={condition}>Renders when condition is true</Show.When>
<Show.Else>Render when condition is not true</Show.Else>
</Show>
</div>
);
}
```

<p class="text-center">fin.</p>
Loading

0 comments on commit 73596fa

Please sign in to comment.