Remix handle 函数是一个有用的对外输入的 Route 模块对象,用于裸露特定的数据 match 对象,它们常常在一起应用。
以后 Remix 版本:1.15.0
在哪里能够定义 handle?
- root 根组件
- 路由页面
在根路由定义
import { /.../ } from "@remix-run/react";// 根路由 handle 配合页面中 useMatches 获取到 app 数据export const handle = { app: 1}export default function App() { return ( <html lang="en"> // ... </html> );
在页面 _index 路由中与 useMatch 一起
handle 与 useMatch 一起应用, useMatch 返回路由匹配相干的对象:
import type { V2_MetaFunction } from "@remix-run/node";// hooksimport { useMatches } from "@remix-run/react";export const meta: V2_MetaFunction = () => { return [{ title: "New Remix App" }];};// 输入定义 handle 对象export const handle = { test: 1,}export default function Index() { const match = useMatches() console.log(match[1].test) // 在 match 中拜访 match 函数 return ( <div> <h1>Welcome to Remix</h1> </div> );}
match 数组
match 是一个数组, 数组中的对象数据结构:
- data: 以后 loader 函数返回的数据
- handle: 以后路由定义的 handle 数据
- id:以后的路由 id
- params: 以后的参数
- pathname: 以后的路由门路
match 个别是一个数组,会有两个对象:
- root.tsx 中的 match 对象
- 以后路由的 match 对象
应用场景
当路由中须要指定一些特定的数据的时候
- Remix-118i 中须要指定 handle
export const handle = { i18n: "login" };
i18n 提供给 Remix-i18n 用于依据以后路由匹配。
援用
- handle
- remix-i18next