-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using with dynamic import such as React.Lazy or react-loadable #25
Comments
It's also mentioned in document. See here :) |
Is this method built-in to React or I should write it by myself? Because if I using React. lazy it may not have this method in React, how should I do? |
@QuanhuQiu |
I have invoked it in List and About component, but it seems like doesn't work. The following message was shown in the console:
Consider adding an error boundary to your tree to customize error handling behavior. Visit https://fb.me/react-error-boundaries to learn more about error . |
@QuanhuQiu Write index.jsx like this: https://codesandbox.io/s/react-live-route-demo-1-ow3d1 import React, {lazy, Suspense} from "react";
import ReactDOM from "react-dom";
import { Route, BrowserRouter, withRouter, Switch } from "react-router-dom";
import NotLiveRoute from "react-live-route";
// import List from "./list";
import Detail from "./detail";
import Bar from "./bar";
// import About from "./about";
import Home from "./home";
import NotFound from "./notFound";
import "./styles.css";
const WaitingList = props => (
<Suspense fallback={<div>Loading...</div>}>
<List {...props} />
</Suspense>
);
const WaitingAbout = props => (
<Suspense fallback={<div>Loading...</div>}>
<About {...props} />
</Suspense>
);
const List = lazy(() => (import('./list')))
const About = lazy(() => (import('./about')))
const LiveRoute = withRouter(NotLiveRoute);
function App() {
return (
<div className="App">
<Switch>
<Route exact path="/" component={Home} />
<Route path="/item/:id" component={Detail} />
<Route path="/items" />
<Route path="/about" />
<Route path="*" render={NotFound} />
</Switch>
<LiveRoute
path="/items"
component={WaitingList}
livePath={"/item/:id"}
name="items"
onHide={(location, match, history, livePath, alwaysLive) => {
console.log("hide hook tiggered");
}}
onReappear={(location, match, history, livePath, alwaysLive) => {
console.log("reappear hook tiggered");
}}
/>
<LiveRoute
path="/about"
alwaysLive={true}
component={WaitingAbout}
name="about"
/>
<Bar />
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
rootElement
);
if ("scrollRestoration" in window.history) {
// 默认值为 'auto'
// window.history.scrollRestoration = 'manual'
}
document.addEventListener("scrollTo", () => {
console.log("document scrolled");
}); |
比较急 我就不用英文了,你那个组件我之前一直用的好好的,但是这次我做用React.lazy Suspense |
|
|
@QuanhuQiu 我已经说了解决方法了啊,是「业务太复杂不能采用这种解决方法」吗 😅 |
我给你提个 pr 行吧!以后万一这个库火了,别忘了我哈 ps:其实我更喜欢黑子 |
业务比较复杂 用了那种方法还是出bug了啊 ,你们现在的项目没做 code splitting吗?你们用的也是react-loadable? 你们下次可以试试在你们的项目里面使用Suspense, React.lazy看看有没有问题啊? |
I have already fixed the bug about React.lazy. you only need to merge my PR and republished again. Thanks a lot. Forgive my poor English... |
@QuanhuQiu Thanks. I notice that you change |
|
@QuanhuQiu 由于我不能复现这个 bug,可以先尝试将 public getRouteDom = () => {
let routeDom
try {
routeDom = ReactDOM.findDOMNode(this)
} catch{}
this.routeDom = (routeDom as CacheDom) || this.routeDom
} 试一下,这个 error 是 react throw 出来的,会破坏整个组件的渲染,感谢 |
|
@QuanhuQiu 在 |
|
@QuanhuQiu 已更,加了 |
|
@QuanhuQiu 赞! |
我用React.lazy Suspense做code-splitting,也遇到无法正常缓存路由,偶尔把上一个的路由里面的dom,都带过来了:下面是我的代码
|
@hzfvictory 使用 |
有用到 ensureDidMount ,在componentDidMount,不过还是那个问题 |
@hzfvictory |
哦哦,放到switch外面,一会我试试 |
https://codesandbox.io/s/react-live-route-demo-1-ozsw4
This is the demo. open the console and try to click more.it can't reappear every time.
The text was updated successfully, but these errors were encountered: