我正在升级到 React v16。发布文档指出“除了少数异常(exception),如果您的应用程序在 15.6 中运行且没有任何警告,则它应该可以在 16 中运行。”我的应用程序在 15.6.2 上运行良好(并且没有警告),但在 16.0.0 上根本不运行,在尝试运行 bundle 时抛出奇怪的错误:
Uncaught TypeError: Cannot read property 'string' of undefined
当它加载一个包文件时 Uncaught TypeError: Cannot read property 'string' of undefined"
当它加载其他。
我怀疑这段代码是不兼容的根源:
ReactDOM.render((
<Router>
<div>
<Route component = { Header } />
<Switch>
<Route exact path = '/' component = { Home } />
<Route exact path = '/index.html' component = { Home } />
<Route path = "/browse" component = { Browse } />
. . . . . . . etc. . . . .
</Switch>
<Route component = { Footer } />
</div>
</Router>
), document.getElementById("main"))
据我所知,我在任何生命周期方法之外渲染它,这段代码是应用程序在我的 webpack 配置文件中的入口点。
如果有用的话,我很高兴附加更多构建脚手架。该应用程序在 15.6.2 下完美运行。
最佳答案
Uncaught TypeError: Cannot read property 'string' of undefined
这看起来是由如下代码引起的
MyComponent.propTypes = {
something: React.PropTypes.string
};
这是因为 PropTypes
在 React 16 中被移到了一个单独的包中:
MyComponent.propTypes = {
something: PropTypes.string
};
此代码确实在 15 中发出警告,因此如果您没有看到警告,则您的设置可能有问题。例如,您可能在开发中错误地使用了 React 的生产版本,因此没有看到警告。
看到您使用了 React Router,请确保将其更新为与 React 16 兼容的版本。最新的 3.x 和 4.x 版本均兼容。
关于javascript - React 应用程序在 15.6.2 上运行良好,在 16.0.0 上中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46702603/