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

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

回答

收藏

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

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

Telegram Mini Apps Navigatorreact-router-dom 的集成.0 t8 _. x$ ]& i3 {

8 g; {' X- e1 {" j安装9 I. }, y" b8 L
pnpm :
2 F% y# C# k' e
  1. pnpm i @telegram-apps/react-router-integration
复制代码
npm :! q5 ]7 d- t. K' q8 V- z6 ]
  1. npm i @telegram-apps/react-router-integration
复制代码
yarn :. r/ V$ V( l! e* h
  1. yarn add @telegram-apps/react-router-integration
复制代码
使用方法

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

下面是它的使用方法:

  1. import { useIntegration } from '@telegram-apps/react-router-integration';' d  q+ a; t( U9 X6 I2 c4 p
  2. import { initNavigator } from '@telegram-apps/sdk-react';
    % _4 J! ?+ X9 H7 }$ ^
  3. import { useEffect, useMemo } from 'react';* D3 E; K5 T: Y
  4. import {; d2 w' H2 P8 q* r) ^( h" B* X6 Y
  5.   Navigate,6 c) s3 m1 V2 N- a5 C4 a+ [
  6.   Route,+ `3 V8 X" E, f/ j5 c* M2 `: v7 Q
  7.   Router,  ?% P# k1 Q- w
  8.   Routes,
    . r  {, z$ h8 g/ s2 l6 d4 w9 n' g
  9. } from 'react-router-dom';2 t& I) P3 C3 W
  10. * z, S! H, [* [% `7 D5 h5 f# c
  11. import { IndexPage } from './IndexPage.js';4 B9 Q9 G0 C: ?6 d+ J
  12. & ]3 s+ V$ B) O
  13. function App() {
    2 N% G3 V( M; J- H, Q1 a% Z7 l. U
  14.   // Create a new application navigator and attach it to the browser history, so it could modify
    . ?9 E% y2 B. ?
  15.   // it and listen to its changes.
    # o6 t. d) p4 ?1 L$ u; O, h' R
  16.   const navigator = useMemo(() => initNavigator('app-navigation-state'), []);
    0 Y$ I  F9 W! x: s- D2 ]
  17.   const [location, reactNavigator] = useIntegration(navigator);$ e% `3 r1 @, K$ _+ M4 i3 A0 T
  18. $ a5 _/ A2 l8 x, v0 \. ?
  19.   // Don't forget to attach the navigator to allow it to control the BackButton state as well
    ) l, z$ I( l! ?: s( {
  20.   // as browser history.
    " @% r" e8 J) k! T
  21.   useEffect(() => {
    % N7 S, a, p- \" U- ]& b# M
  22.     navigator.attach();% ?& x+ s' F( `+ v# c3 W: r4 s# Q
  23.     return () => navigator.detach();: l7 O/ o, p& {- w
  24.   }, [navigator]);) q$ `# L) o; \7 j: L

  25. + u" q0 G) R9 Q$ i  H
  26.   return (
    9 X! \3 j2 v  o7 t9 m/ J& U" @
  27.     <Router location={location} navigator={reactNavigator}>4 ]+ U* d- _. G7 y7 l
  28.       <Routes>& }' @5 W# m7 |  ?, z
  29.         <Route path={'/'} component={IndexPage}/>
    6 D1 e0 X/ M. C6 S& W$ i& u
  30.         <Route path={'*'} element={<Navigate href={'/'}/>}/>9 \7 V( A0 ]6 A) N5 Z
  31.       </Routes>8 F4 _4 n" u" m* b  I4 ^
  32.     </Router>
      d1 O6 x8 [6 N' p: G5 J
  33.   );
    ( Y: W  p' g) b$ w9 r3 P1 a
  34. }
复制代码

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


& A8 E5 Y" v8 q3 v. J. A; F. W8 R) E8 a5 g

) L" W# y2 |* X" |1 Q( l) v& Z& R* I8 r# L% G' O" ?

; e0 @( [" ^' N' l2 s( d
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则