【发布时间】:2016-12-10 11:08:11
【问题描述】:
我在尝试使用 React 渲染 HTMLDivElement 时遇到了这个问题,但是我收到以下错误:
Uncaught Invariant Violation: Objects are not valid as a React child (found: [object HTMLDivElement]). 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 `VirtualList`
在我的 React Component 类中,我有这段代码...
import React, { Component } from 'react';
export default class VirtualList extends Component {
constructor(props) {
super(props);
this.container = document.createElement('div');
}
renderList() {
let container = this.container;
// Manipulate container etc.
return container;
}
render() {
return (
<div>
{this.renderList()}
</div>
)
}
}
我们将不胜感激!
【问题讨论】:
-
你能解释一下为什么要使用
document.createElement吗? -
处理dom不是react方式。
-
@AlexanderT。我写了一些简单的 javascript 代码,背后有一堆逻辑,所以我想在某种意义上将该代码移植到 React 中,认为你可以只渲染 HTMLDivElement 对象