reactjs - ReactJS:在测试 HOC 组件时,渲染没有返回任何内容
问题描述
我有以下代码:
TableWrapper.jsx
const TableWrapper = props => {
return (
<div>
{props.table}
</div>
);
}
然后它被用于Foo.jsx
export class FooTable extends React.Component {
render() {
return (
<div>
<TableWrapper
table={<ListTable />}
/>
</div>
);
}
}
这是ListTable.jsx
render() {
const {data, error, asyncStatus} = this.props.instanceList;
return (
<div>
<CustomTable
title='123'
/>
</div>
)
}
我使用 jest 和酵素进行其中一项测试:
it('Simulate search input field', () => {
const container = mount(<FooTable {...mockProps} />);
});
});
我正进入(状态
console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Uncaught [Invariant Violation: TableWrapperComponent(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.]
如果我使用shallow
我不会出错。我错过了什么?
解决方案
您应该使用渲染,然后将您的组件放入您的渲染道具中。
<TableWrapper render={table => <ListTable props={...this.props} />} />
现在我会问您在 ListTable 中使用的 CustomTable 在哪里?在某些时候,您将不得不制作一个初始组件或使用许多库中的一个。
const myGenericComponent = (props) => {
// create variables deconstruct props as you like
const name = props.name
return (
// assuming I passed in name from my parent component as one of my props
<div><table><th>Table {name}</th><td>{name}</td></table>
)
}
export default myGenericComponent;
推荐阅读
- svn - Tortoise SVN - 无法连接到 URL ' ' 处的存储库,SSL 通信期间发生错误
- javascript - 带有 emoji-mart 的 window.getSelection() 问题(Picker 组件)
- mysql - “where 子句”中有一个未知列“用户名”
- python - 如何在 Matplotlib 中制作 groupby 条形图
- reactjs - 在反应中实施redux后地图功能不起作用
- php - 从表中获取活动和非活动记录
- android - 错误:当我在数据库中有更多表时,AppDatabase_Impl.java 中的代码太大
- angular - 更新操作期间在 Angular 中管理本地存储数据的问题
- react-native - 如何在 React Native 中实现 Twilio SDK 调用?
- ios - 谷歌/Facebook 退出不是持久的