Skip to content

Commit

Permalink
feat: add content to new post
Browse files Browse the repository at this point in the history
  • Loading branch information
minmaxw1024 committed Nov 17, 2024
1 parent 8c806f6 commit e085fb9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
4 changes: 2 additions & 2 deletions posts/how_to_use_google_font.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ The benefits of using Google Font are:
Go to [Google Fonts](https://fonts.google.com/?preview.text=Arcane%20Tome).
Type what you want to display in the textarea, and find the font you like. For example, I choose [Grenze](https://fonts.google.com/?preview.text=Arcane%20Tome&query=Grenze). It's a serif font, a little vintage style, I think it's suitable for my blog.

![google front](/static/posts//google_font_01.png)
![google front](/static/posts/google_font_01.png)

### Step 2: Add the font to your project

Click the "Get font" button, and select "Get embed code". You can select what styles do you want to use, and copy the code. In my fresh project, I put the codes in `_layout.tsx` like this:

![google front](/static/posts//google_font_02.png)
![google front](/static/posts/google_font_02.png)

```tsx
import { type PageProps } from "$fresh/server.ts";
Expand Down
53 changes: 53 additions & 0 deletions posts/relearn_react_03.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,56 @@ snippet: Relearning React from react.dev
---

## From "Conditional Rendering"

Conditional rendering may be the most used feature in daily React development. It allows you to render different components or elements based on a condition.

If you return `null` from a component, nothing will be rendered.

And using ternary operator is a common way to conditionally render elements.

Using `&&` is another way to conditionally render elements. It works because in JavaScript, `true && expression` always evaluates to `expression`, and `false && expression` always evaluates to `false`.

```jsx
// isPacked is true, render "✅", or render nothing.
return (
<li className="item">
{name} {isPacked && ""}
</li>
);
```

Using variables to store elements is another way to conditionally render elements.

## Pitfalls

### Using `&&` for Conditional Rendering

Don’t put numbers on the left side of &&.

To test the condition, JavaScript converts the left side to a boolean automatically. However, if the left side is 0, then the whole expression gets that value (0), and React will happily render 0 rather than nothing.

For example, a common mistake is to write code like `messageCount && <p>New messages</p>`. It’s easy to assume that it renders nothing when `messageCount` is 0, but it really renders the 0 itself!

To fix it, make the left side a boolean: `messageCount > 0 && <p>New messages</p>`.

## Deep Dive

### What's the difference between these two code snippets?

```jsx
if (isPacked) {
return <li className="item">{name} ✅</li>;
}
return <li className="item">{name}</li>;
```

```jsx
return <li className="item">{isPacked ? name + "" : name}</li>;
```

> JSX elements aren’t “instances” because they don’t hold any internal state and aren’t real DOM nodes. So these two examples, in fact, are completely equivalent.
## Rendering Lists

- [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) (Don't Repeat Yourself)
- [Logical And &&](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND)

0 comments on commit e085fb9

Please sign in to comment.