- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于 a long time,在遇到错误边界后,我一直试图让路由在我们的应用程序中工作,但直到今天我才发现看似与周围许多示例相同的代码有一个重要区别:路由被包裹一个 Switch
。如果启用,这个简单的更改足以阻止路由工作。 Demo
采取以下代码段。如果我删除 Switch
位,即使每个组件都应该失败,这也可以正常工作,但如果被开关包裹则不会。我想知道为什么。
<div style={{ backgroundColor: "#ffc993", height: "150px" }}>
<Switch>
<Route
path="/"
exact
render={() => (
<ErrorBoundary>
<MyComponent1 title="Component 1" />
</ErrorBoundary>
)}
/>
<Route
path="/comp1"
render={() => (
<ErrorBoundary>
<MyComponent1 title="Component 1 Again" />
</ErrorBoundary>
)}
/>
<Route
path="/comp2"
render={() => (
<ErrorBoundary>
<MyComponent2 title="Component 2" />
</ErrorBoundary>
)}
/>
</Switch>
最佳答案
基本上,这个问题归结为 React 如何 reconciliation .
When a component updates, the instance stays the same, so that state is maintained across renders. React updates the props of the underlying component instance to match the new element
<App>
<Switch>
<Route path="a" component={Foo}/>
<Route path="b" component={Foo}/>
</Switch>
</App>
Foo
的相同实例。两条路线!一个
<Switch>
将始终返回第一个匹配的元素,所以基本上当 React 渲染时,这相当于树
<App><Foo/></App>
对于路径“a”和
<App><Foo/></App>
对于路径“b”。如果 Foo 是一个有状态的组件,这意味着状态会被保留,因为该实例只是传递了新的 props (在我们的例子中,除了
children
之外,没有任何 props),并且期望通过重新计算它自己的来处理这个状态。
In order to reset the value when moving to a different item (as in our password manager scenario), we can use the special React attribute called key. When a key changes, React will create a new component instance rather than update the current one. (...) In most cases, this is the best way to handle state that needs to be reset.
The way I would recommend resetting this error boundary (if you really want to blow away the error) would be to just clear it out using a new key value. (...) This will tell React to throw out the previous instance (with its error state) and replace it with a new instance.
key
的替代方法s 将实现暴露一些可以在外部调用的钩子(Hook)或尝试检查
children
改变属性(property),这很难。像这样的东西可以工作(
demo):
componentDidUpdate(prevProps, prevState, snapshot) {
const childNow = React.Children.only(this.props.children);
const childPrev = React.Children.only(prevProps.children);
if (childNow !== childPrev) {
this.setState({ errorInfo: null });
}
key
Prop :-)
关于reactjs - 错误边界禁用交换机内部的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59056211/
如何收集设备/交换机上的带宽使用情况/利用率。据我所知,有些系统可以做这样的事情。他们似乎都有 snmp 的共同点。 我正在寻找有关可能滚动我自己的系统来收集这些数据的信息,这些数据稍后将用于基于 W
下表给出的 DES-3528 DLink 交换机的 OID 是什么 DES-3528:admin#show snmp comm 命令:显示 snmp 社区 SNMP 社区表 社区名称查看名称访问权限
我正在使用 Java ganymed 库通过 SSH2 连接到我们的交换机。我可以毫无问题地连接 Catalyst 交换机,但是当我连接到 Nexus 交换机时,我无法从命令中获得任何输出。 有人用过
我需要使用 php 远程登录到 cisco 交换机并执行 show interface status命令并获得结果。我尝试了一些在互联网上找到的 php 类,但它们都无法连接到设备。所以我尝试自己编写
这个问题已经有答案了: React Router v4 renders multiple routes (3 个回答) 已关闭 5 年前。 我见过各种例子,说当使用React Router V4时,你
我目前正在尝试编写一个脚本来更改 200 多台 Cisco 交换机的位置 (snmp)。 我的问题是我一次不能运行多个命令。我制作了一个批处理文件,它自动连接到交换机并读取列出命令的 .txt 文件。
我正在使用此代码 https://gist.github.com/svett/b7f56afc966a6b6ac2fc作为起点。 使用它并将其指向 cisco 路由器会得到以下错误消息: 拨号失败:s
我最近进入了 PhantomJS 目前正在使用 PhantomJS 来完成我的第一份开发工作。 我的任务是从通过 LAN 电缆连接到 PC 的旧 Cisco Catalyst 2960 x 交换机上抓
日前,IDC 发布的《2020 年网络市场跟踪报告》显示,2020 年全球网络市场规模为 490.1 亿美元,与去年同期相比下滑 3%,其中交换机、路由器和 WLAN 市场增速分别为 - 3.5%、
我是一名优秀的程序员,十分优秀!