# react-native: Branch URL: https://generaltranslation.com/ja/docs/react-native/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` の取り得る値に対応するために、props として `black`、`brown`、`blonde` を定義しています。 ```jsx title="BranchExample.jsx" copy import { Branch } from 'gt-react-native'; 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-native'; 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]
); } ``` ### Branch の翻訳 コンテンツを翻訳するには、`` コンポーネントで囲みます。 ```jsx title="BranchExample.jsx" copy import { T, Branch } from 'gt-react-native'; 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-native'; 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-native/guides/variables)など、ほかのコンポーネントと組み合わせることで、表現力が高くローカライズされたインターフェースを作成できます。 ## 次のステップ * より高度な使用方法や例については、[Using Branching Components](/docs/react-native/guides/branches)を参照してください。