更好地调试控制台输出以反应StatelessComponent的错误

时间:2015-11-20 13:44:43

标签: javascript reactjs react-jsx stateless

新反应0.14 StatelessComponent很不错。但是,当其render方法出错时,react仅将其打印到控制台:

Uncaught Error: Invariant Violation: Objects are not valid as a React child 
(found: Mon Nov 23 2015 06:00:00 GMT+0100 (Central Europe Standard Time)). If 
you meant to render a collection of children, use an array instead or wrap the 
object using createFragment(object) from the React add-ons. Check the render 
method of `StatelessComponent`.

有没有办法用displayName注释组件?

1 个答案:

答案 0 :(得分:4)

通过尝试找到解决方案:将displayName添加到组件函数中:

const component = (props) => {
  return (<div />)
}
component.displayName = "MyComponent"
module.exports = component

然后,按照原样做出反应,将使用此displayName。那种你期望从伟大的反应中获得的东西;)