作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在测试一个包装器组件,它使用 children Prop 渲染它的 child 。问题是在测试文件中,当我设置包装器时,它工作正常。但是当我在组件中克隆子 Prop 时,出现以下错误:
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, or you might have mixed up default and named imports.
这仅在使用 mount 而不是 shallow 时发生,我希望它与 mount 一起使用。
class Parent extends React.Component {
render() {
const { someProps, someOtherProps, anotherProp } = this.props;
return (
<ParentWrapper someInfo={someProps} >
<One someOtherProps={someOtherProps} />
<Two anotherProp={anotherProp} />
</ParentWrapper>
);
}
}
父包装器:
class ParentWrapper extends React.Component {
render() {
const {children, somethin} = this.props;
const clonedChildren = React.cloneElement(children, R.merge(children.props, { somethin }));
return (
<div>
{clonedChildren}
</div>
);
}
}
测试文件:
const Parent = require('parent');
panel = mount(<Parent {...defaultProps} />);
最佳答案
这个我也遇到过。 SO 上的大多数其他类似答案都涉及正确的导入/导出,但是当我使用 shallow()
时它工作正常,这意味着它不是导入/导出。
最终我发现我的 react
和 react-dom
版本略有不匹配。 (提醒,因为我需要它,请删除现有的 node_modules
和 package-lock.json
以确保您实际上正在拉取更新的模块)。这为我修复了它。
关于reactjs - 不变违规 : Element type is invalid: expected a string (for built-in components) when testing with enzyme mount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52795416/
与其说这是一个技术问题,不如说是一个政策问题。 开发应用程序时,如果附属机构提供的链接构成您内容的一部分,您是否可以显示该链接?例如,专门用于从 Affiliate Window 查找特定流派黑胶唱片
我是一名优秀的程序员,十分优秀!