小编Ar2*_*r26的帖子

React Router v6 - 没有 Outlet 的嵌套路由

我正在使用react-router v6,是否可以在不使用Outlet组件的情况下使用嵌套路由?

例如:我有一个用户页面和 onClick 用户组件,它应该导航到特定的用户页面,

当前尝试:

 <Routes>
   <Route path="/" element={<Home />} />
   <Route path="/users" element={<Users />} />
     <Route path=":id" element={<UserPage/>} />
 </Routes>
Run Code Online (Sandbox Code Playgroud)

然后单击特定用户:

const gotoUserPage = () => {
  navigate('1')
}
Run Code Online (Sandbox Code Playgroud)

gotoUserPage 将 URL 从“/users”更改为“/users/1”,但 userPage 组件不会呈现。

reactjs react-router react-router-dom

11
推荐指数
2
解决办法
1万
查看次数

React 路由器 - useOutletContext 测试

我正在使用react-router V6并尝试测试useOutletContext的新功能。我的测试库是testing-library/react,我不确定如何在测试中传递上下文数据。

在 TSX 组件中,我使用 React-router 的钩子获取数据:

const { data } = useOutletContext<IContext>()
Run Code Online (Sandbox Code Playgroud)

我需要类似的东西:

test("render outlet context data view", async () => {
  const { getByTestId } = render(
    <MockedProvider mocks={[mockData]} context={myContextData}>
       <ContextDataView />
    </MockedProvider>
)
Run Code Online (Sandbox Code Playgroud)

MockedProvider 标签来自@apollo/client/testing

context={myContextData} 部分是我需要的

reactjs react-router apollo-client react-testing-library

7
推荐指数
2
解决办法
4285
查看次数

Apollo Client Reactive 变量 - 更新值后不触发重新渲染

我正在尝试使用反应变量:

我的缓存文件:

import { makeVar } from "@apollo/client"

export const colorVar = makeVar('red')
Run Code Online (Sandbox Code Playgroud)

文件 A(更新值):

import { colorVar } from "cache"
colorVar('green')
Run Code Online (Sandbox Code Playgroud)

文件 B(读取值并应在文件 A 中更新值后重新渲染):

import { colorVar } from "cache"
console.log(colorVar())
Run Code Online (Sandbox Code Playgroud)

文件 A 中的更新操作不会触发文件 B 的重新渲染

reactjs apollo-client reactive-variable

3
推荐指数
2
解决办法
1040
查看次数

React redirect - 在react-router-dom上重定向组件

我正在尝试构建一个简单的组件,它将进行一次 API 调用,并在 2 秒后重定向到另一个页面。为此,我尝试使用react-router-dom中的重定向组件,但导入失败

import { Redirect } from "react-router-dom"
Run Code Online (Sandbox Code Playgroud)

我收到错误:“react-router-dom 没有导出成员‘重定向’。”

版本“react-router-dom”:“^6.0.0-beta.0”,

redirect reactjs react-router

2
推荐指数
1
解决办法
1246
查看次数

Intl.supportedValuesOf 类型“typeof Intl”上不存在属性“supportedValuesOf”

我使用 dayJS 作为我的日期时间库,并希望呈现所有时区的列表。
我尝试使用 Intl 对象:
Intl.supportedValuesOf("timeZone")
但出现打字稿错误:
TS2339: Property 'supportedValuesOf' does not exist on type 'typeof Intl'.
我可以获取值,但我想避免使用 @ts-ignore 表示法

如何为 Intl 对象添加更新的类型?

typescript

2
推荐指数
1
解决办法
2006
查看次数