- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用react-router 4.0.0设置一个通用的react应用程序。
服务器端渲染工作正常,除了我的链接标记在服务器和客户端上的渲染方式不同之外。这是错误消息:
Warning: React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to compensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server:
(client) o Furb</h1><a href="./" data-reactid="4"
(server) o Furb</h1><a href="/./" data-reactid="4
我对 react 路由器和 react 非常陌生,所以请记住,这可能是我的代码中的一个小错误。另外,我正在边学习边学习,所以,我的代码中可能还有其他严重的错误,提前抱歉。
在网上查找时,我想出了这个错误修复程序,它涵盖了类似的问题:https://github.com/ReactTraining/react-router/pull/4484
但是,这个补丁包含在react-router 4.0.0中(我戳了一下代码,它确实修复了)。因此,我的问题不可能是由此引起的。
经过大量修改后,我怀疑我的配置或由于我对自己正在做的事情了解有限而产生的错误。例如,对于 StaticRouter,我使用 location={req.originalUrl}
而不是 location={req.url}
作为 req.url
似乎总是 /
,我不知道为什么。
我在下面发布了我认为相关的文件,但如果您想检查完整的代码,您可以在这里找到它:https://github.com/magp/furb/tree/universal
package.json
{
"name": "furb",
"version": "0.0.1",
"description": "A boilerplate for Firebase-UniversalRedux-Bootstrap/MaterialUI projects",
"main": "app/server.jsx",
"scripts": {
"start": "concurrently --kill-others \"npm run start:api\" \"npm run start:back\"",
"start:back": "node app/serverlauncher.js",
"start:api": "node api/server.js",
"clean": "rimraf static",
"serve:prod": "http-server ./static -o -p 3032",
"build:app": "NODE_ENV=production webpack --config config/webpack-prod-config.js",
"build": "npm run clean && npm run build:app && npm run serve:prod",
"test": "echo 'NODE_ENV=production mocha './tests/**/*.spec.js' --compilers js:babel-core/register'",
"lint": "eslint --config=./.eslintrc app/**/**/*.jsx"
},
"repository": {
"type": "git",
"url": "git+https://github.com/magp/furb.git"
},
"keywords": [
"Boilerplate",
"React",
"Redux",
"UniversalJS",
"Bootstrap",
"MaterialUI"
],
"author": "magp",
"license": "MIT",
"bugs": {
"url": "https://github.com/magp/furb/issues"
},
"homepage": "https://github.com/magp/furb#readme",
"dependencies": {
"express": "^4.15.2",
"history": "^4.6.1",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-hot-loader": "^3.0.0-beta.6",
"react-router-dom": "^4.0.0"
},
"devDependencies": {
"babel-core": "^6.24.0",
"babel-loader": "^6.4.0",
"babel-preset-es2015": "^6.24.0",
"babel-preset-react": "^6.23.0",
"concurrently": "^3.4.0",
"eslint": "^3.17.1",
"eslint-config-airbnb": "^14.1.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^4.0.0",
"eslint-plugin-react": "^6.10.0",
"html-webpack-plugin": "^2.28.0",
"http-server": "^0.9.0",
"node-sass": "^4.5.0",
"rimraf": "^2.6.1",
"sass-loader": "^6.0.3",
"style-loader": "^0.14.0",
"webpack": "^2.2.1",
"webpack-dev-middleware": "^1.10.1",
"webpack-hot-middleware": "^2.17.1"
}
}
应用程序/serverlauncher.js
require('babel-register')({
presets: [ 'es2015', 'react' ]
});
var app = require('./server.jsx');
应用程序/server.jsx
var path = require('path');
var express = require('express');
var React = require('react');
var ReactDOMServer = require('react-dom/server');
var StaticRouter = require('react-router-dom/StaticRouter').default;
var webpack = require('webpack');
var config = require('../config/webpack-dev-config');
var Routes = require('./routes/Routes.jsx').default;
var app = express();
var compiler = webpack(config);
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: config.output.publicPath
}));
app.use(require('webpack-hot-middleware')(compiler));
const routes = [
'/',
'/about'
];
app.use('*', function (req, res, next) {
const context = {};
const componentHTML = ReactDOMServer.renderToString(<StaticRouter context={context} location={req.originalUrl}><Routes /></StaticRouter>);
const HTML = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Static</title>
</head>
<body>
<div id="app">${componentHTML}</div>
<script type="text/javascript" src="/static/source.js"></script>
</body>
</html>
`;
res.end(HTML);
});
const PORT = process.env.PORT || 3030;
app.listen(PORT, 'localhost', function(err) {
if (err) {
console.log(err);
return;
}
console.log('Listening at http://localhost:3030');
});
app/routes/Routes.jsx
import React from 'react';
import { Route, Switch } from 'react-router-dom';
import List from '../components/List';
import About from '../components/About';
import NotFound from '../components/NotFound';
import links from '../../api/data/links.json';
const linksarr = Object.keys(links).map(function(k) { return links[k] });
function Routes() {
return (
<Switch>
<Route exact path="/" render={props => (<List links={linksarr} {...props} />)} />
<Route exact path="/about" component={About} />
<Route component={NotFound} />
</Switch>
);
}
export default Routes;
应用程序/组件/Layout.jsx
import React from 'react';
import { Link } from 'react-router-dom';
function Layout() {
return (
<div>
<h1>Welcome to Furb</h1>
<Link to="./">Home</Link>
<Link to="./about">About</Link>
<Link to="./test">Test</Link>
</div>
);
}
export default Layout;
请原谅这个长问题,并提前感谢您提供的任何帮助。
最佳答案
尝试在链接的“收件人”字段中删除句点。它应该只是“/”、“/about”等,而不是“./about”。路由不是文件路径,它们由路由器解析和解释,并且句点会使它感到困惑。
import React from 'react';
import { Link } from 'react-router-dom';
function Layout() {
return (
<div>
<h1>Welcome to Furb</h1>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
<Link to="/test">Test</Link>
</div>
);
}
export default Layout;
关于javascript - StaticRouter 为 <Link> 标签呈现前导斜杠 [react-router 4.0.0],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42863308/
我是一名优秀的程序员,十分优秀!