Javascript createMemoryHistory仍报告`浏览器历史记录需要DOM`
我收到了我认为不应该出现的错误:) 流程是 ClientJavascript createMemoryHistory仍报告`浏览器历史记录需要DOM`,javascript,reactjs,redux,react-router,react-router-v4,Javascript,Reactjs,Redux,React Router,React Router V4,我收到了我认为不应该出现的错误:) 流程是 Clienthistory.jsClient/index.jshistory=createBrowserHistory{ 返回{routes} } App.propTypes={ 历史记录:PropTypes.object } 导出默认应用程序 浏览器路由器组件创建自己的历史对象,在非DOM环境(如服务器)中,它始终是不工作的 您可以将应用程序的结构设置为不包括服务器上的浏览器路由器,也可以将其替换为组件,并将其传递给您自己的历史对象,该对象将在非DO
history.js
Client/index.js
history=createBrowserHistory
<
Serverhistory.js
Server/index.js
history=createMemoryHistory
我可以确认服务器上正在使用内存,然后内存会下降,因此我们无法访问BroswerHistory
server.js中出现错误
Invariant Violation: Browser history needs a DOM
30 |
31 | // Render the component to a string
> 32 | const markup = renderToString(
33 | <Provider store={store}>
34 | <App history={history} />
35 | </Provider>
我已经检查并确保
也没有放错地方
routes.js
const路由=(
)
导出默认路由
server.js
从“../common/history”导入历史记录
常量存储=配置存储(预加载状态、历史记录)
//将组件渲染为字符串
常量标记=renderToString(
)
App.js
从“React”导入React
从“道具类型”导入道具类型
从“已连接路由器”导入{ConnectedRouter}
从“../routes”导入路由
常量应用=({history})=>{
返回{routes}
}
App.propTypes={
历史记录:PropTypes.object
}
导出默认应用程序
浏览器路由器组件创建自己的历史
对象,在非DOM环境(如服务器)中,它始终是不工作的
您可以将应用程序的结构设置为不包括服务器上的浏览器路由器
,也可以将其替换为组件,并将其传递给您自己的历史
对象,该对象将在非DOM环境中使用内存历史
const routes = (
<Fragment>
<Router history={history}>
<Switch>
<Route component={NoMatch} />
</Switch>
</Router>
</Fragment>
)
const路由=(
)
在哪里使用导出的历史
对象?您不应该将其与路由器
一起使用,而不是使用浏览器路由器
<代码>代码>导出的历史
在多个位置使用,服务器/index.js
,客户端/index.js
,然后作为道具通过这两个服务器/客户端
文件传递到App.js
。考虑将<代码> <代码>改为<代码> <代码>,看看它是否有效。你看到这个问题了吗?也许根本原因是相似的@我使用的是连接的react路由器v5
,这只是一个说明。这确实奏效了:D如果可能的话,请添加答案并简要解释原因:D Hanks pal,我不知道这让它成为自己的历史
,很高兴知道。
const routes = (
<Fragment>
<BrowserRouter>
<Switch>
<Route component={NoMatch} />
</Switch>
</BrowserRouter>
</Fragment>
)
export default routes
import history from '../common/history'
const store = configureStore(preloadedState, history)
// Render the component to a string
const markup = renderToString(
<Provider store={store}>
<App history={history} />
</Provider>
)
import React from 'react'
import PropTypes from 'prop-types'
import { ConnectedRouter } from 'connected-react-router'
import routes from '../routes'
const App = ({ history }) => {
return <ConnectedRouter history={history}>{routes}</ConnectedRouter>
}
App.propTypes = {
history: PropTypes.object
}
export default App
const routes = (
<Fragment>
<Router history={history}>
<Switch>
<Route component={NoMatch} />
</Switch>
</Router>
</Fragment>
)