【问题标题】:React Enzyme Type Error:Cannot read property 'propTypes' of undefined反应酶类型错误:无法读取未定义的属性“propTypes”
【发布时间】:2016-08-02 13:49:45
【问题描述】:

以下是我的 React 组件:

import React from 'react'

var PageLeftLower = React.createClass({
  render:function(){
    return(<a href="#">Quote Requests</a>);
  }
});

module.exports = PageLeftLower;

所以,非常简单的 React 组件。 我刚刚开始使用 Enzyme 和 Mocha 进行测试,并编写了以下代码。

import expect from 'expect';
import React from 'react';
import {shallow} from 'enzyme';
import {PageLeftLower} from './PageLeftLower';

describe('Component : WholeTab',() => {
  it('renders without exploding', () => {
    expect(shallow(<PageLeftLower/>).length).toEqual(1); 
  });
});

当我执行它时,它会输出以下警告:

组件: WholeTab 警告:React.createElement:类型不应为 null、未定义、布尔值或数字。它应该是一个字符串(对于 DOM 元素)或一个 ReactClass(对于复合组件)。

还有以下错误:

TypeError:无法读取未定义的属性“propTypes”。

非常感谢任何帮助。

【问题讨论】:

    标签: javascript node.js reactjs mocha.js enzyme


    【解决方案1】:

    问题就在这里:

    import {PageLeftLower} from './PageLeftLower';
    

    因为您使用 :

    直接导出组件
    module.exports = PageLeftLower;
    

    它没有被包裹在这样的对象中:

    module.exports = {PageLeftLower: PageLeftLower};
    

    因此您的组件可以通过以下方式访问:

    import PageLeftLower from './PageLeftLower'; // not {PageLeftLower}
    

    【讨论】:

    • 完美的男人,我昨天想通了,但感谢您的回答:)
    猜你喜欢
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 2018-07-01
    • 1970-01-01
    相关资源
    最近更新 更多