无法使用服务器端渲染访问DOM-react 0.14.1、react DOM 0.14.1和react router 1
Cannot access DOM with server-side render - react 0.14.1, react-dom 0.14.1 and react-router 1.0.0-rc3
我无法使用react、react DOM和react router的服务器实现访问DOM。我要么有ReferenceError:文档未定义,要么浏览器历史记录需要DOM错误。
服务器条目:
module.exports = function( req, res, next ) {
match({ routes, location: req.url }, (error, redirectLocation, renderProps) => {
if (error) {
res
.status(500)
.send(error.message);
} else if (redirectLocation) {
res.redirect(302, redirectLocation.pathname + redirectLocation.search);
} else if (renderProps) {
res
.status( 200 )
.set( 'Content-Type', 'text/html' )
.send( '<!doctype html>' +
renderToString(
[ <RoutingContext {...renderProps} />,
<HtmlDocument /> ]
)
);
} else {
res
.status(404)
.send('Not found');
}
})
};
client.js:
import { render } from 'react-dom';
import routes from './routes';
render( routes, document.getElementById('app') )
routes.jsx:
import React from 'react';
import { Router, Route, IndexRoute } from 'react-router';
import createBrowserHistory from 'history/lib/createBrowserHistory';
import Application from './Application';
import Index from './pages/index';
import App from './pages/app';
import Auth from './pages/auth';
import Login from './pages/auth/components/Login';
import Signup from './pages/auth/components/Signup';
import NotFound from './pages/notFound';
var routes = (
<Router history={createBrowserHistory()}>
<Route path="/" component={Application}>
<IndexRoute component={Index} />
<Route path="app" component={App} onEnter={ App.requireAuth } />
<Route path="auth" component={Auth} />
<Route path="signup" component={Signup} />
<Route path="login" component={Login} />
<Route path="*" component={NotFound} />
</Route>
</Router>
);
export default routes;
提前感谢您的帮助。
再次查看React Router服务器渲染指南。您只能在客户端上渲染具有浏览器历史记录的<Router>
;在服务器上,您只需将路由(而不是<Router>
)传递给<RoutingContext>
。
相关文章:
- React DOM offsetHeight before rendering
- 如何让React JS点击处理程序在执行时更新DOM
- React.js如何使用虚拟DOM加速渲染
- 从子级获取React dom组件
- DOM Attributes and React
- React - 从 DOM 元素获取组件以进行调试
- React:是否可以访问DOM元素's道具
- React:将组件附加到DOM
- React-从简单对象树创建dom
- 来自jQuery,如何在React.js中制作DOM动画?例如设置进度条的动画
- react-在不破坏封装的情况下访问DOM
- React.JS在虚拟DOM中计算img宽度
- 更改表结构会导致 React 失去对 DOM 的跟踪
- ReactJS:如何从 DOM 更新 React
- 无法使用服务器端渲染访问DOM-react 0.14.1、react DOM 0.14.1和react router 1
- 尝试使用 React.DOM 来设置正文样式
- 错误:无法解析模块'react-dom'
- 如何有条件地添加属性到react DOM元素
- react dom/server是否在客户端工作
- React.DOM.p(null, message) - explain null?