ブログに技術的な記事を書いてからかなりの時間が経ちましたが、ここでは @shopify/restyle と expo を使用して React Native で型強制 UI コンポーネントを構築することに関する新しい記事を紹介します。
@shopify/restyle は、UI コンポーネントにタイプ セーフティと一貫性をもたらす React Native 用の強力なスタイル ライブラリです。従来のスタイル設定アプローチとは異なり、Restyle を使用すると、アプリケーション全体にデザイン システムの原則を適用する一元化されたテーマ構成を作成できます。
はじめる
プロジェクトのセットアップ
- expo を使用して反応ネイティブ プロジェクトをセットアップする
npx create-expo-app@latest
- プロジェクト ディレクトリに移動し、expo を使用して @shopify/restyle パッケージをインストールします。
cd /path/to/project npx expo install @shopify/restyle
テーマの作成
デザイン システムを定義するための theme.tsx ファイルを作成します。
touch theme.tsx
- デフォルトのテーマ設定をコピーして貼り付けます
import {createTheme} from '@shopify/restyle'; const palette = { purpleLight: '#8C6FF7', purplePrimary: '#5A31F4', purpleDark: '#3F22AB', greenLight: '#56DCBA', greenPrimary: '#0ECD9D', greenDark: '#0A906E', black: '#0B0B0B', white: '#F0F2F3', }; const theme = createTheme({ colors: { mainBackground: palette.white, cardPrimaryBackground: palette.purplePrimary, }, spacing: { s: 8, m: 16, l: 24, xl: 40, }, textVariants: { header: { fontWeight: 'bold', fontSize: 34, }, body: { fontSize: 16, lineHeight: 24, }, defaults: { // We can define a default text variant here. }, }, }); export type Theme = typeof theme; export default theme;
テーマプロバイダーの実装
app/_layout.tsx を更新します:
import { DarkTheme, DefaultTheme } from "@react-navigation/native"; import { useFonts } from "expo-font"; import { Stack } from "expo-router"; import * as SplashScreen from "expo-splash-screen"; import { StatusBar } from "expo-status-bar"; import { useEffect } from "react"; import "react-native-reanimated"; import { ThemeProvider } from "@shopify/restyle"; import theme from "@/theme"; // Prevent the splash screen from auto-hiding before asset loading is complete. SplashScreen.preventAutoHideAsync(); export default function RootLayout() { const [loaded] = useFonts({ SpaceMono: require("../assets/fonts/SpaceMono-Regular.ttf"), }); useEffect(() => { if (loaded) { SplashScreen.hideAsync(); } }, [loaded]); if (!loaded) { return null; } return ( <themeprovider theme="{theme}"> <stack> <stack.screen name="(tabs)" options="{{" headershown: false></stack.screen> <stack.screen name="+not-found"></stack.screen> </stack> <statusbar> <h2> Creating Reusable Components </h2> <h3> Text Component </h3> <pre class="brush:php;toolbar:false">touch components/Text.tsx
// In components/Text.tsx import {createText} from '@shopify/restyle'; import {Theme} from '../theme'; export const Text = createText<theme>(); </theme>
ホーム画面に使ってみましょう
import { Text } from "@/components/Text"; import { SafeAreaView } from "react-native-safe-area-context"; export default function HomeScreen() { return ( <safeareaview> <text margin="m" variant="header"> This is the Home screen. Built using @shopify/restyle. </text> </safeareaview> ); }
上記のコードでわかるように、マージンを数値ではなく「m」として渡しています。値は、theme.tsxfile から取得しています。
// ./theme.tsx const theme = createTheme({ spacing: { s: 8, m: 16, // margin="m" l: 24, xl: 40, }, textVariants: { header: { // our text header variant fontWeight: 'bold', fontSize: 34, }, body: { fontSize: 16, lineHeight: 24, }, }, // ...rest of code }, });
ホームページのビューは次のようになります
スケルトンローダーコンポーネント
スケルトン ローダー カードを作成しましょう
touch components/SkeletonLoader.tsx
// components/SkeletonLoader.tsx import { BackgroundColorProps, createBox, createRestyleComponent, createVariant, spacing, SpacingProps, VariantProps, } from "@shopify/restyle"; import { Theme } from "@/theme"; import { View } from "react-native"; const Box = createBox<theme>(); type Props = SpacingProps<theme> & VariantProps<theme> & BackgroundColorProps<theme> & React.ComponentProps<typeof view>; const CardSkeleton = createRestyleComponent<props theme>([ spacing, createVariant({ themeKey: "cardVariants" }), ]); const SkeletonLoader = () => { return ( <cardskeleton variant="elevated"> <box backgroundcolor="cardPrimaryBackground" height="{20}" marginbottom="s" width="70%" overflow="hidden" borderradius='{"m"}'> </box> <box backgroundcolor="cardPrimaryBackground" height="{100}" marginbottom="s" width="90%" overflow="hidden" borderradius='{"m"}'> </box> <box backgroundcolor="cardPrimaryBackground" height="{50}" marginbottom="s" width="70%" overflow="hidden" borderradius='{"m"}'> </box> </cardskeleton> ); }; export default SkeletonLoader; </props></typeof></theme></theme></theme></theme>
- @shopify/restyle パッケージから定義済みコンポーネントとして新しいボックスを作成します。これがスケルトン ボックスの作成方法になります。
const Box = createBox<theme>(); </theme>
- createStyleComponent を使用して新しい CardSkeleton コンポーネントを作成し、カスタム コンポーネントを作成します。また、theme.tsx ファイルで定義する必要があるスペーシングと CardVariants である props を渡しました。
type Props = SpacingProps<theme> & VariantProps<theme> & BackgroundColorProps<theme> & React.ComponentProps<typeof view>; const CardSkeleton = createRestyleComponent<props theme>([ spacing, createVariant({ themeKey: "cardVariants" }), ]); </props></typeof></theme></theme></theme>
- SkeletonLoader コンポーネントを作成して、Skelton Card コンポーネントをレンダリングします。
// components/SkeletonLoader.tsx export const SkeletonLoader = () => { return ( <cardskeleton variant="elevated"> <box backgroundcolor="cardPrimaryBackground" height="{20}" marginbottom="s" width="70%" overflow="hidden" borderradius='{"m"}'></box> <box backgroundcolor="cardPrimaryBackground" height="{100}" marginbottom="s" width="90%" overflow="hidden" borderradius='{"m"}'></box> <box backgroundcolor="cardPrimaryBackground" height="{50}" marginbottom="s" width="70%" overflow="hidden" borderradius='{"m"}'></box> </cardskeleton> ); };
これを機能させるために残っていることは 1 つあります。theme.tsx ファイルを更新して CardVariants を含めます
const theme = createTheme({ colors: { // Add Black Color to use it later on black: palette.black, }, // Add Border Radius Variants borderRadii: { s: 4, m: 10, l: 25, xl: 75, }, // Add Card Variants cardVariants: { elevated: { shadowColor: "black", shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.1, shadowRadius: 4, elevation: 3, borderRadius: "m", }, defaults: { padding: "m", borderRadius: "m", }, }, });
それは素晴らしいですが、コンポーネントにアニメーションを付けてみましょう
// components/SkeletonLoader.tsx const ShimmerAnimation = () => { const shimmerTranslate = useRef(new Animated.Value(0)).current; useEffect(() => { Animated.loop( Animated.timing(shimmerTranslate, { toValue: 1, duration: 1500, useNativeDriver: true, }) ).start(); }, [shimmerTranslate]); const translateX = shimmerTranslate.interpolate({ inputRange: [0, 1], outputRange: [-300, 300], }); return ( <animated.view> <p>and let’s use it in our Skeleton Loader Component<br> </p> <pre class="brush:php;toolbar:false">// components/SkeletonLoader.tsx export const SkeletonLoader = () => { return ( <cardskeleton variant="elevated"> <box backgroundcolor="cardPrimaryBackground" height="{20}" marginbottom="s" width="70%" overflow="hidden" borderradius='{"m"}'> <shimmeranimation></shimmeranimation> </box> <box backgroundcolor="cardPrimaryBackground" height="{100}" marginbottom="s" width="90%" overflow="hidden" borderradius='{"m"}'> <shimmeranimation></shimmeranimation> </box> <box backgroundcolor="cardPrimaryBackground" height="{50}" marginbottom="s" width="70%" overflow="hidden" borderradius='{"m"}'> <shimmeranimation></shimmeranimation> </box> </cardskeleton> ); };
完全なコンポーネント コードは次のとおりです。
// components/SkeletonLoader.tsx import { useEffect, useRef } from "react"; import { Animated } from "react-native"; import { BackgroundColorProps, createBox, createRestyleComponent, createVariant, spacing, SpacingProps, VariantProps, } from "@shopify/restyle"; import { Theme } from "@/theme"; import { View } from "react-native"; const Box = createBox<theme>(); const ShimmerAnimation = () => { const shimmerTranslate = useRef(new Animated.Value(0)).current; useEffect(() => { Animated.loop( Animated.timing(shimmerTranslate, { toValue: 1, duration: 1500, useNativeDriver: true, }) ).start(); }, [shimmerTranslate]); const translateX = shimmerTranslate.interpolate({ inputRange: [0, 1], outputRange: [-300, 300], }); return ( <animated.view> <p>Et voila, we made a skeleton loader card using @shopify/restyle using </p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173310152233378.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="How to build type-enforced UI components in React Native using @shopify/restyle"></p> <h2> Support for dark mode </h2> <p>Let’s start with adding dark theme configuration, in your theme.tsxfile<br> </p> <pre class="brush:php;toolbar:false">// theme.tsx export const darkTheme: Theme = { ...theme, colors: { ...theme.colors, mainBackground: palette.white, cardPrimaryBackground: palette.purpleDark, greenPrimary: palette.purpleLight, }, textVariants: { ...theme.textVariants, defaults: { ...theme.textVariants.header, color: palette.purpleDark, }, },
layout.tsx ファイルにダークテーマの設定を追加して、アプリのレイアウトにダークテーマの設定を追加します
npx create-expo-app@latest
- カラー スキーマに基づいて、デフォルトのライト テーマを使用するか、ダーク モードでは、theme.tsx ファイルで定義されている darkTheme 構成を使用します。
// app/_layout.tsx テーマ、{ darkTheme } を "@/theme" からインポートします。 //... 残りのコード <themeprovider darktheme :> <stack.screen name="(tabs)" options="{{" headershown: false></stack.screen> <stack.screen name=" 見つかりません"></stack.screen> スタック> <p>これがダークモードとライトモードです。</p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173310152340178.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="How to build type-enforced UI components in React Native using @shopify/restyle"></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173310152557660.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="How to build type-enforced UI components in React Native using @shopify/restyle"></p> <p>ほら、@shopify/restyle パッケージを使用してタイプ強制 UI コンポーネントを作成できました</p> <p>ありがとうございます:)</p> </themeprovider>
以上が@shopify/restyle を使用して React Native で型強制 UI コンポーネントを構築する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

Javaandjavascriptaredistinctlanguages:javaisusedforenterpriseandmobileapps、whilejavascriptisforinteractivewebpages.1)javaiscompiled、staticatically、andrunsonjvm.2)javascriptisisterted、dynamsornoded.3)

JavaScriptコアデータ型は、ブラウザとnode.jsで一貫していますが、余分なタイプとは異なる方法で処理されます。 1)グローバルオブジェクトはブラウザのウィンドウであり、node.jsのグローバルです2)バイナリデータの処理に使用されるNode.jsの一意のバッファオブジェクト。 3)パフォーマンスと時間の処理にも違いがあり、環境に従ってコードを調整する必要があります。

javascriptusestwotypesofcomments:シングルライン(//)およびマルチライン(//)

PythonとJavaScriptの主な違いは、タイプシステムとアプリケーションシナリオです。 1。Pythonは、科学的コンピューティングとデータ分析に適した動的タイプを使用します。 2。JavaScriptは弱いタイプを採用し、フロントエンドとフルスタックの開発で広く使用されています。この2つは、非同期プログラミングとパフォーマンスの最適化に独自の利点があり、選択する際にプロジェクトの要件に従って決定する必要があります。

PythonまたはJavaScriptを選択するかどうかは、プロジェクトの種類によって異なります。1)データサイエンスおよび自動化タスクのPythonを選択します。 2)フロントエンドとフルスタック開発のためにJavaScriptを選択します。 Pythonは、データ処理と自動化における強力なライブラリに好まれていますが、JavaScriptはWebインタラクションとフルスタック開発の利点に不可欠です。

PythonとJavaScriptにはそれぞれ独自の利点があり、選択はプロジェクトのニーズと個人的な好みに依存します。 1. Pythonは、データサイエンスやバックエンド開発に適した簡潔な構文を備えた学習が簡単ですが、実行速度が遅くなっています。 2。JavaScriptはフロントエンド開発のいたるところにあり、強力な非同期プログラミング機能を備えています。 node.jsはフルスタックの開発に適していますが、構文は複雑でエラーが発生しやすい場合があります。

javascriptisnotbuiltoncorc;それは、解釈されていることを解釈しました。

JavaScriptは、フロントエンドおよびバックエンド開発に使用できます。フロントエンドは、DOM操作を介してユーザーエクスペリエンスを強化し、バックエンドはnode.jsを介してサーバータスクを処理することを処理します。 1.フロントエンドの例:Webページテキストのコンテンツを変更します。 2。バックエンドの例:node.jsサーバーを作成します。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

SublimeText3 中国語版
中国語版、とても使いやすい

ドリームウィーバー CS6
ビジュアル Web 開発ツール

VSCode Windows 64 ビットのダウンロード
Microsoft によって発売された無料で強力な IDE エディター

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境
