我最近遇到了一个相当讨厌的错误,其中代码是<select>通过JavaScript动态加载的.这种动态加载<select>具有预先选择的值.在IE6中,我们已经有了修复所选内容的代码<option>,因为有时它<select>的selectedIndex值与所选<option>的index属性不同步,如下所示:
field.selectedIndex = element.index;
Run Code Online (Sandbox Code Playgroud)
但是,此代码无效.即使selectedIndex正确设置了字段,最终也会选择错误的索引.但是,如果我alert()在正确的时间插入声明,则会选择正确的选项.考虑到这可能是某种时间问题,我尝试了一些随机的东西,我之前在代码中看到过:
var wrapFn = (function() {
var myField = field;
var myElement = element;
return function() {
myField.selectedIndex = myElement.index;
}
})();
setTimeout(wrapFn, 0);
Run Code Online (Sandbox Code Playgroud)
这有效!
我已经找到了解决问题的方法,但是我很不安,因为我不知道为什么这会解决我的问题.有人有官方解释吗?使用"稍后"调用我的功能可以避免哪些浏览器问题setTimeout()?
我有一个具有以下功能的反应组件
const handleNavigate = (clientId) => {
console.log(clientId)
navigate(`/dashboard/clients/${clientId}`)
}
Run Code Online (Sandbox Code Playgroud)
console.log() 显示了我想要附加以在导航功能中使用的 ID。
并且
浏览器中的 URL正在更新。
但页面没有变化。
这适用于从 导航/dashboard/clients到/dashboard/clients/foo
但无法从 导航/dashboard/clients/foo到/dashboard/clients/bar
clientId 像这样传递到卡中......
const CompanyCard = (props) => {
const { client, showWatchlist, removeDisabled, showRemove, removeType } = props
...
}
Run Code Online (Sandbox Code Playgroud)
然后在卡里
<CardActionArea
onClick={() => handleNavigate(client._id)}
...
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
谢谢
在阅读了 @redapollos 的建议后,我尝试了Outlet,
useRoutes 方法...都不起作用。
import { useRoutes } from 'react-router-dom'
// then in the routes...
{ path: 'clientdetail/:id', element: …Run Code Online (Sandbox Code Playgroud) 我们正在开发一个用 dotnet 编写的带有 Razor 视图(纯后端堆栈)的项目。我们计划将每个视图移至React用于react-router-dom处理客户端和服务器中的路由。
我们的想法是一页一页地移动,而不是大爆炸,因为该项目已经在生产中。
客户端路由器
import { Router } from 'react-router-dom'
import { createBrowserHistory, History } from 'history'
const history: History = createBrowserHistory({ basename: baseUrl })
<Router history={history}>
<Switch>
<Route path="/example" component={Example} />
...
</Switch>
</Router>
Run Code Online (Sandbox Code Playgroud)
服务器路由器
import { StaticRouter } from 'react-router-dom'
<StaticRouter
basename={basename}
context={routerContext}
location={params.location.path}
>
<Switch>
<Route path="/example" component={Example} />
...
</Switch>
</StaticRouter>
Run Code Online (Sandbox Code Playgroud)
问题
/当我从(在 React 路由器中)导航到/example(在 .net 路由器中)然后尝试返回到 时/example,看起来没有转换到 React 路由器,并且浏览器历史记录保持在/。
我想使用 React Router 点击刷新页面。
我知道,我可以用window.location.reload();
但我想使用 React Router 的一些魔法。有一种方法可以使用 React Router 刷新页面,还是应该使用 window 对象中的普通方法?