ReactRouter:警告:React.createElement:types不能为null,未定义

警告Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components). Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components).

然后它抛出错误Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.renderToString

整个代码是:

 import { renderToString } from 'react-dom/server'; import * as React from "react"; import * as ReactDOM from "react-dom"; let History = require("history"); let ReactRouter = require("react-router"); let match = ReactRouter.match; let RouterContext = ReactRouter.RouterContext; let Route = ReactRouter.Route; let Router = ReactRouter.Router; let Link = ReactRouter.Link; let TestComponent = React.createClass({ render: function() { return ( <button>test</button> ); } }); console.log(renderToString(<TestComponent/>));// a let routes = [ <Route path="/" component={TestComponent}/> ]; match({ routes, location: "/" }, (error, redirectLocation, renderProps) => { if (error) { console.log(error); return; } if (redirectLocation) { console.log(redirectLocation); return; } if (renderProps) { let component = <RouterContext {...renderProps} />;// b console.log(renderToString(component));// c } }); 

a处的输出是正确的。

警告在b

错误在c

环境:

  • react-router:1.0.3
  • 历史:1.17.0
  • 反应和反应:0.14.3

该代码基于https://github.com/rackt/react-router/blob/master/docs%2Fguides%2Fadvanced%2FServerRendering.md中的文档

       

网上收集的解决方案 "ReactRouter:警告:React.createElement:types不能为null,未定义"