无法使用服务器端渲染访问 DOM - react 0.14.1、react-dom 0.14.1 和 react-router 1.0.0-rc3
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: document is not defined,或者 Browser history needs a DOM errors.
服务器条目:
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 server rendering guide。您只在客户端呈现带有浏览器历史记录的 <Router>
;在服务器上,您只需将 routes(而不是 <Router>
)传递给 <RoutingContext>
.
我无法通过服务器实现 react、react-dom 和 react-router 访问 DOM。我有 ReferenceError: document is not defined,或者 Browser history needs a DOM errors.
服务器条目:
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 server rendering guide。您只在客户端呈现带有浏览器历史记录的 <Router>
;在服务器上,您只需将 routes(而不是 <Router>
)传递给 <RoutingContext>
.