不变式违例:元素types无效:期望一个string(用于内置组件)或类/函数。 反应路由器v4

我对reactjs比较陌生,正在使用react-router v4testing基本的服务器端渲染,但是我无法越过这个错误,从几个小时以来一直在尝试。 我已经尝试了我在谷歌find的每一个解决scheme,但他们似乎没有工作。

这里是server.js代码:

import Express from 'express'; import React from 'react'; import { renderToString } from 'react-dom/server'; import { StaticRouter } from 'react-router' import MyRoutes from './routes/routes.js'; ... app.get('*', (req, res) => { let markup = `<!DOCTYPE html> <html lang="en"> <body> ${renderToString(<StaticRouter location={req.url} context={{}}><MyRoutes/></StaticRouter>)} </body> </html>`; res.write(markup); res.end(); }); 

问题似乎与以下代码:

./routes/routes.js代码:

 import React from 'react'; import { Match, Miss } from 'react-router'; const componentTest = () => ( <div> Testing a component </div> ); export default () => ( <div> <Match exactly={true} pattern="/" component={componentTest} /> </div> ); 

现在, 如果我删除匹配标记线,我得到空白页,没有错误。 但是,如果匹配标签行在那里,我得到以下错误:

 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. in Unknown in Router (created by StaticRouter) in StaticRouter Invariant Violation: 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. Check the render method of `StatelessComponent`. at invariant (/home/ubuntu/workspace/node_modules/react-dom/node_modules/fbjs/lib/invariant.js:44:15) at instantiateReactComponent (/home/ubuntu/workspace/node_modules/react-dom/lib/instantiateReactComponent.js:74:56) at instantiateChild (/home/ubuntu/workspace/node_modules/react-dom/lib/ReactChildReconciler.js:44:28) at /home/ubuntu/workspace/node_modules/react-dom/lib/ReactChildReconciler.js:71:16 at traverseAllChildrenImpl (/home/ubuntu/workspace/node_modules/react-dom/lib/traverseAllChildren.js:77:5) at traverseAllChildren (/home/ubuntu/workspace/node_modules/react-dom/lib/traverseAllChildren.js:172:10) at Object.ReactChildReconciler.instantiateChildren (/home/ubuntu/workspace/node_modules/react-dom/lib/ReactChildReconciler.js:70:7) at ReactDOMComponent.ReactMultiChild.Mixin._reconcilerInstantiateChildren (/home/ubuntu/workspace/node_modules/react-dom/lib/ReactMultiChild.js:187:41) at ReactDOMComponent.ReactMultiChild.Mixin.mountChildren (/home/ubuntu/workspace/node_modules/react-dom/lib/ReactMultiChild.js:226:27) at ReactDOMComponent.Mixin._createContentMarkup (/home/ubuntu/workspace/node_modules/react-dom/lib/ReactDOMComponent.js:653:32) 

任何帮助将非常感激。

解决这个问题的是Tharaka Wijebandara在评论中给出的。

问题是Match已经被react-router v4 Route所取代。 所以用这行replaceMatch标签行解决了它:

  <Route exact path="/" component={componentTest} /> 

来源: https : //reacttraining.com/react-router/web/api/Route