# gt-react: General Translation React SDK: Branch
URL: https://generaltranslation.com/ja/docs/react/api/components/branch.mdx
---
title: Branch
description: Branch コンポーネントの API リファレンス
---
{/* 自動生成: 直接編集せず、代わりに content/docs-templates/ 内のテンプレートを編集してください。 */}
## 概要
`` コンポーネントを使うと、翻訳に条件分岐を追加できます。
```jsx
const status = 'active';
ユーザーはアクティブです。
}
inactive={ユーザーは非アクティブです。
}
/>
```
`branch` パラメータに値を渡すと、指定したキーに基づいて対応する出力値が選択されます。
## リファレンス
### Props
`[key]: string` 構文は、使用可能なブランチを表す任意のキーを指定できることを示します。
たとえば、`active` や `inactive` のようなブランチをパラメータとして追加できます。
| Prop | Description |
| --------------- | ------------------------------------------------------------------------- |
| `branch` | レンダリングするブランチ名。 |
| `children` | 一致するブランチが見つからない場合にレンダリングするフォールバックコンテンツ。 |
| `[key]: string` | 指定されたブランチ値に応じて表示するコンテンツを表すブランチ。各キーがそれぞれのブランチに対応し、その値がレンダリングされるコンテンツになります。 |
### 戻り値
指定されたブランチに対応するコンテンツ、またはフォールバックコンテンツを含む `JSX.Element`。
### 例外
`branch` prop が指定されていない、または無効な場合は、`Error` をスローします。
## 例
### 基本的な使い方
`` では、`branch` prop に指定される値ごとに、異なる出力を用意する必要があります。
この例では、`user.hairColor` の値に応じて出力を決定します。
`user.hairColor` の取り得る値に対応するために、`black`、`brown`、`blonde` の props を定義しています。
```jsx title="BranchExample.jsx" copy
import { Branch } from 'gt-react';
export default function HairColor({ user }) {
return (
Their hair is dark.}
brown="Their hair is in the middle." // (文字列を渡すことも可能です)
blonde={Their hair is light.
}
/>
);
}
```
### フォールバックコンテンツ
`branch` に渡された値に一致する prop がない場合は、常に `children` がフォールバックとして使用されます。
```jsx title="BranchExample.jsx" copy
import { Branch } from 'gt-react';
export default function HairColor({ user }) {
return (
Their hair is dark.}
brown={Their hair is in the middle.
}
blonde={Their hair is light.
}
>
Their hair is unknown.
// [!code highlight]
);
}
```
### ブランチの翻訳
コンテンツを翻訳するには、`` コンポーネントで囲みます。
```jsx title="BranchExample.jsx" copy
import { T, Branch } from 'gt-react';
export default function HairColor({ user }) {
return (
// [!code highlight]
Their hair is dark.}
brown={Their hair is in the middle.
}
blonde={Their hair is light.
}
>
Their hair is unknown.
// [!code highlight]
);
}
```
### 変数の追加
ブランチ内で動的な値を表示する場合は、``、``、``、または``コンポーネントでそれらを囲んでください。
```jsx title="BranchExample.jsx" copy
import { Branch, T, Var } from 'gt-react';
export default function HairColor({ user }) {
return (
Their hair is dark.}
brown={Their hair is in the middle.
}
blonde={Their hair is light.
}
>
Unhandled hair color: {user.hairColor}
// [!code highlight]
);
}
```
***
## 注記
* ブランチのキーには、branch prop に一致する任意の文字列値を使用できます。この柔軟性により、`` を幅広いユースケースに合わせて簡単に利用できます。
* `` を、翻訳用の `` や動的コンテンツ用の[変数コンポーネント](/docs/react/guides/variables) など、他のコンポーネントと組み合わせることで、表現力が高くローカライズされたインターフェースを作成できます。
## 次のステップ
* より高度な使い方や具体例については、[Branching Components の使い方](/docs/react/guides/branches)を参照してください。