【发布时间】:2016-08-24 16:53:51
【问题描述】:
我编写了一个函数来基于对象的键和值构建 HTML 表单,我试图在我的 render 方法中返回该表单。但是,我不断收到错误消息:
ReactJs 0.14 - Invariant Violation: Objects are not valid as a React child
这是我的createForm() 方法:
createForm() {
const obj = {
}
const object_fields = resourceFields.fields;
let form = document.createElement('form');
_.forIn(object_fields, function(field_value, field_name) {
let div = document.createElement('div');
div.setAttribute('className', 'form-control');
let label = document.createElement('label');
label.setAttribute('htmlFor', 'name');
label.innerHTML = field_name;
let input = document.createElement('input');
input.setAttribute('className', 'form-control');
input.setAttribute('type', 'text');
input.setAttribute('ref', field_name);
input.setAttribute('id', field_name);
input.setAttribute('value', field_value);
input.setAttribute('onChange', '{this.handleChange}');
div.appendChild(label);
div.appendChild(input);
form.appendChild(div);
})
console.log(form) //this prints out fine
return form
}
这是我的render() 方法:
render() {
return (
<div>
{this.createForm()}
</div>
)
}
有人知道会发生什么吗?我的表单在控制台中打印出来就好了...提前致谢!
【问题讨论】: