- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个 React 应用程序,我不想在其中更新浏览器中的 URL。我没有使用“react-router-dom”,而只使用“react-router”和 MemoryRouter(https://reacttraining.com/react-router/web/api/MemoryRouter)。 history.push() 可直接在组件语句中使用,但我希望将历史传递给这些主要组件的子组件的子组件,但该属性未定义。
这是主 App.js 中的路由器部分(组件 Home 和 ScreeningTool 可以按预期访问 this.props.history.push()):
...
import {Route, MemoryRouter} from "react-router";
...
<MemoryRouter>
<Route exact path="/" component={Home} />
<Route path="/screening-tool" component={ScreeningTool} />
</MemoryRouter>
...
Home 和 ScreeningTool 都使用子组件 AppLink,它生成一个“链接”以在 Home 和 ScreeningTool 之间导航,就像这样(注意我将“历史”作为 Prop 传递):
Home.js:
...
<AppLink
url="/"
label="Screening Tool"
history={this.props.history}
/>
...
AppLink.js:
...
<div className="pseudo-link">
<span onClick={() => this.props.history.push(this.props.url)}>
{this.props.label}
</span>
</div>
...
上面的代码有效。但是 Home 中有子组件,它们将创建自己的 AppLinks 以及曾孙组件。我不想将 history 属性作为组件 Prop 从 Parent 传递到 Child 到 GrandChild 组件,因为这似乎效率不高。我发现了以下 stackoverflow 问题,但这些选项都不适合我:
this.props.history.push works in some components and not others
react-router getting this.props.location in child components
我尝试了上面第二个 URL 中描述的更新的“userHistory”:
...
import { useHistory } from 'react-router';
...
render() {
let history = useHistory();
return (
<div className="pseudo-link">
<span onClick={() => history.push(this.props.url)}>
{this.props.label}
</span>
</div>
);
}
...
但我得到 Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
.
我尝试使用 withRouter
如此处定义 https://reacttraining.com/react-router/web/api/withRouter但我得到 Error: Invariant failed: You should not use <withRouter(PseudoLink) /> outside a <Router>
.
最后,this.props.history.push works in some components and not others 的可接受答案以代码块 export default withRouter(connect(mapStateToProps, matchDispatchToProps)(ChildView));
结尾但没有解释 mapStateToProps 或 matchDispatchToProps 从何而来?
我认为问题在于我使用的是 MemoryRouter 而不是来自“reacto-router-dom”的普通/最常见路由器。
谁能帮帮我?
最佳答案
useHistory
是一个 Hook,因此它应该在功能组件中使用,而不是在基于类的组件中使用。
最后,this.props.history.push 的公认答案在某些组件中有效,而在其他组件中无效,以代码块 export default withRouter(connect(mapStateToProps, matchDispatchToProps)(ChildView)); 结尾;但没有解释 mapStateToProps 或 matchDispatchToProps 从何而来?
-如果你不使用 redux 那么你可以使用
export default withRouter(yourComponentName);
更新
我已经将 AppLink
组件更改为此并且它工作正常
import React from "react";
import "./AppLink.css";
import { useHistory } from 'react-router';
const AppLink = props => {
const history = useHistory();
return (
<div
className="app-link-button"
onClick={() => history.push(props.url)}
>
<span>{props.label}</span>
</div>
);
};
export default AppLink;
关于javascript - React/Router/MemoryRouter - 如何传递历史属性并在子组件中使用 push()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61031252/
我想测试我的组件是否可以获取位置的搜索部分并转换为 json 以供使用,但是当我使用查询字符串设置 initialEntries 时,搜索部分没有改变。 //My Test render(
我正在为 React 应用程序编写快照测试,并且我发现当我使用 编写测试时会出现这种情况。在它们中,它发出警告,我需要有一个独特的关键 Prop 。 当我查看react-router文档时,没有提到
在我的项目中,我必须在不更改 url 的情况下从一个组件导航到另一个组件。为此,我使用了 MemoryRouter这按预期工作。但现在同样的想法应该适用于不同的路线,即 /one和 /two .例如,
我已经密切关注这些例子,但我无法得到 内存路由器 (这是您应该如何测试路由组件?)使用 Jest 和 enzyme 进行测试。 我想导航到其中一条路线,并将其反射(reflect)在我的快照中。下面的
我正在尝试弄清楚如何使用测试来断言当前位置路径名是什么。 作为实验and based on some docs我写了下面的测试 test(`Can navigate MemoryRouter`,
我有一个名为 Component.ts 的假设 React 组件,其代码结构类似于: import React, { FC } from 'react'; import { useParams } f
我正在构建一个 React 应用程序,我不想在其中更新浏览器中的 URL。我没有使用“react-router-dom”,而只使用“react-router”和 MemoryRouter(https:
我正在尝试测试一个组件,该组件通过在 Enzyme 中创建一个包装器来监听正在更改的历史记录,该包装器将我的组件放置在 MemoryRouter 中;即: mount(
我的任务是测试依赖于上层某处的 BrowserRouter 组件的 React 组件。据我所知,BrowserRouter 将路由对象传递给任何子对象、孙对象等的 props。 组件: import
如有重复请见谅 我知道 MemoryRouter 有 initialEntries 和 initialIndex,所以你可以为“location”和“history”设置路径等。但是“匹配”没有得到更
我正在尝试测试我的路由器是否按预期工作。但是我无法让路由器指向除 / 之外的其他位置 这是我的简化测试代码。 App.tsx import React from 'react'; import {Ro
所以,我正在尝试使用 MemoryRouter 进行测试,如果我点击一个应该将我带到新的Route 的按钮,它实际上会打开该路由. 所以“ConversationView”是 Conversation
将 React Router DOM (v6) 与 React 测试库结合使用,我想知道是否有办法处理来自 createMemoryHistory 的“模拟”历史记录或类似的替代方法。 基于Testi
我一直在尝试让测试适用于react-router v4。我已经采取了记录的基本example here并为其编写了以下测试: import React from 'react'; import { T
我正在使用 React 构建单页 Web 应用程序。对于此应用程序,我想防止 URL 更改,并避免使用 href 链接以防止“链接预览”出现在浏览器的左下角。 为了解决我的问题的前半部分,我发现在应用
我正在使用react-router-dom并找到很有用。但是,我仍然希望许多路由能够在浏览器中从 URL 读取/写入。我应该如何实现这个? 谢谢 最佳答案 最好将您的应用程序包装在 BrowserRo
我是一名优秀的程序员,十分优秀!