English 简体中文 繁體中文 한국 사람 日本語 Deutsch русский بالعربية TÜRKÇE português คนไทย french

简体中文 繁體中文 English 日本語 Deutsch 한국 사람 بالعربية TÜRKÇE คนไทย Français русский

回答

收藏

Telegram 小程序 | TypeScript @telegram-apps/react-router-integration

开源社区 开源社区 10328 人阅读 | 0 人回复 | 2025-02-25

Telegram Mini Apps Navigatorreact-router-dom 的集成.
' k1 _) A' R7 j" S. D" g" e( f
2 _, t5 p- l. Y6 ]0 S( o  G5 V安装; p; v* R1 z- C
pnpm :
! z. ?$ |  ]* ]# P; @4 y
  1. pnpm i @telegram-apps/react-router-integration
复制代码
npm :. c. }0 A+ O6 _) a. I" E
  1. npm i @telegram-apps/react-router-integration
复制代码
yarn :, p) k, m% w2 |# x2 d  [# ]
  1. yarn add @telegram-apps/react-router-integration
复制代码
使用方法

目前,该软件包只提供一个函数,用于创建 react-router-dom 路由器的集成 - useIntegration。

下面是它的使用方法:

  1. import { useIntegration } from '@telegram-apps/react-router-integration';7 q2 ?  o4 t6 R2 H* D
  2. import { initNavigator } from '@telegram-apps/sdk-react';
    2 H# g0 r" Y  W) j! B. U
  3. import { useEffect, useMemo } from 'react';
    & z) W+ i' X6 J8 `3 z" B) i3 b6 M
  4. import {: G9 R- h) ?1 y2 E& H3 m
  5.   Navigate,
    . m' v  ]# Z) c+ z- H0 v9 i& `
  6.   Route,
    & E# K# i& X/ L# f: w" Y3 t
  7.   Router,6 Q1 V- r$ Q) Q7 Y- ]4 q
  8.   Routes,  q' J  i5 }( F) U. b) r
  9. } from 'react-router-dom';4 N2 \9 r* A3 o% R2 _
  10. 6 M+ O! r' s$ n2 Y
  11. import { IndexPage } from './IndexPage.js';$ {. _* b$ |* d. W& W7 x& P

  12. 3 Y) T8 k: l+ H6 N3 j' m; H
  13. function App() {
    % b7 w- C* Z- g5 l
  14.   // Create a new application navigator and attach it to the browser history, so it could modify7 m1 L) G2 R$ a6 h% R- e: m
  15.   // it and listen to its changes.
    8 B+ e' R: I5 e9 P: d$ {
  16.   const navigator = useMemo(() => initNavigator('app-navigation-state'), []);
    $ o9 L/ R: z. \  T; }. ?: w% w  U
  17.   const [location, reactNavigator] = useIntegration(navigator);0 C( r: Z$ m3 d# u9 I1 G* H

  18. 2 \6 `4 P, [2 a6 r8 u
  19.   // Don't forget to attach the navigator to allow it to control the BackButton state as well) n9 }1 a0 i( j+ s6 ^* V' s: u
  20.   // as browser history.5 I- m4 k6 s0 y
  21.   useEffect(() => {% T/ N' H9 \' ^$ b, I; t- H- ~
  22.     navigator.attach();/ X) l1 Q& ^8 M0 L, B
  23.     return () => navigator.detach();
    - ^5 g' c; x7 ^, P4 Z; g3 j
  24.   }, [navigator]);9 C# q3 `( M5 R

  25. ! Z! _. q2 J  v. S+ P9 \
  26.   return (
    8 b/ }4 }/ u" a- u
  27.     <Router location={location} navigator={reactNavigator}>
    % P" m! v: V6 q) g5 T
  28.       <Routes>3 J# q, H( N. j5 _9 L# e4 x
  29.         <Route path={'/'} component={IndexPage}/>
    ! d$ j* }& W4 r% W# h2 Y
  30.         <Route path={'*'} element={<Navigate href={'/'}/>}/>
    / r7 F: f, p0 H
  31.       </Routes>
    5 @; ~, n' R  @1 H8 G
  32.     </Router>
    7 n6 c4 ]) Q1 J/ @$ I, }$ }
  33.   );' k: ?1 O" Z$ u. A
  34. }
复制代码

您可以通过 我们的 React 模板 了解更多关于如何使用它的实际应用。


# n" A9 s" k* I- n% b4 C: p: q: q& D& w' y* X

- u/ {+ t! t1 O; ?7 r; i, C6 L3 x. j1 I: A, s

, N6 O+ c( p* V$ M& G: T
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则