使用React Router 4进行服务器端渲染时,上下文为未定义
我正在使用React Router 4在基本级别上实现服务器端渲染,以了解它是如何工作的。 不过,我遇到了一些问题,React Router提供的文档并不清楚如何解决它们:
1)/服务器/路由中的我的上下文variables是未定义的。
在前面的教程中我看到React Router用来使用createServerRenderContext(); 但是在他们的文档中没有提到,所以我假设他们摆脱了它。 我认为StaticRouter应该为你创build你的上下文吗? 我如何定义我的上下文?
2)我的htmlvariables呈现StaticRouter作为一个string在控制台显示这两个错误:
Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in. Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in.
我不完全确定为什么这是一个问题。 我相信这是正确的导出,我通过renderToString传递。 为什么这给我一个错误?
任何帮助深表感谢! 这是我的代码如下:
服务器端
/server/server.js
import express from 'express' import router from './routes/index.js' const app = express(); app.use(express.static('public')); app.use(router) const PORT = 3000; app.listen(PORT, function(){ console.log('Listening on ' + PORT) });
/server/routes/index.js
import { Router } from 'express' import React from 'react' import ReactDOMServer from 'react-dom/server' import { StaticRouter } from 'react-router' import App from '../../client/App' const router = Router(); router.get('*', function(request, response) { const context = {} const html = ReactDOMServer.renderToString( <StaticRouter location={request.url} context={context}> <App/> </StaticRouter> ); if (context.url) { response.writeHead(301, { Location: context.url }) response.end() } else { response.write(html) response.end() } }); export default router
客户端
/client/index.js
import React from 'react' import ReactDOM from 'react-dom' import { BrowserRouter } from 'react-router'; import App from './App' const props = window.PROPS; ReactDOM.render( <BrowserRouter> <App {...props} /> </BrowserRouter>, document );
/client/App.js
import React from 'react' class App extends React.Component { constructor() { super() } handleClick() { alert() } render() { return ( <html> <head> <title>{this.props.title}</title> <link rel="stylesheet" href="/style.css" /> </head> <body> <main> <h1>{this.props.title}</h1> <p>This is some text that makes up a paragraph</p> <button onClick={this.handleClick}>Click me</button> </main> <script dangerouslySetInnerHTML={{ __html: 'window.PROPS=' + JSON.stringify(this.props) }} /> <script src="/bundle.js" /> </body> </html> ) } } export default App;
Webpack.config.js
var webpack = require('webpack'); var path = require('path'); module.exports = { entry: './src/client/index.js', output: { filename: 'bundle.js', path: path.resolve(__dirname, 'public'), publicPath: '/public/' }, module: { rules: [ { test: /\.js?$/, exclude: /node_modules/, include: path.join(__dirname, 'src'), use: [ { loader: 'babel-loader', query: { presets: ['react', ['es2015', { 'modules': false }], 'stage-0'], plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy'] } } ] } ] } };
- 我的第一个Node.js服务器:无法加载资源:net :: ERR_INCOMPLETE_CHUNKED_ENCODING
- PhantomJS没有正确地提供JS和CSS文件
- 具有socket.io的单页应用程序中特定于部分的侦听器
- 使用Node Express和Passport捕获锚链接/路由
- 为Node.js服务器+ SPA javascript框架设置自动化BDDtesting环境的简单方法?
- 无法在Nodejs中执行fileupload程序
- 预期的响应包含一个数组,但有一个对象
- 如何用Express后台构build多个布局的Angular JS
- Angular2 POST Observable Request返回一个不可读的JSON对象