简体   繁体   English

将DOM元素插入React组件

[英]Insert DOM element into React component

I am using django as backend and most of it as frontend as well, the exception is one form that is made in reactjs. 我将django用作后端,并且大部分也用作前端,例外是reactjs中制作的一种形式。 To speed up loading of this form I would like to load it's data by grabing it from html instead of using request. 为了加快此表单的加载速度,我想通过从html抓取而不是使用request来加载它的数据。 Django would send the data and form on loading the page and another request would not be neccessary. Django会在加载页面时发送数据和表单,而无需其他请求。 I managed to grab the data, which is html, but I am running in to following error: 我设法获取了html数据,但遇到以下错误:

Uncaught (in promise) Error: Objects are not valid as a React child (found: [object HTMLOptionElement]). 未捕获(承诺)错误:对象无效,不能作为React子对象(找到:[object HTMLOptionElement])。 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. 如果您打算渲染孩子的集合,请改用数组或使用React附加组件中的createFragment(object)包装对象。 Check the render method 检查渲染方法

For testing purposes I am grabing html from another form that looks like this: 为了进行测试,我从另一个看起来像这样的表单中获取html:

<select name="tz" border="solid 1px #F6892D" id="id_tz">
  <option value="Africa/Abidjan">Africa/Abidjan</option>
  (...)
  <option value="" selected=""></option>
</select>

It happens after the component mounted: 它在组件安装后发生:

componentDidMount = () => {
    document.addEventListener("DOMContentLoaded",  (event) => { 
        var tz_options = document.getElementById('id_tz');
        this.setState({tz:Array.from(tz_options)});
    });
}

I also tried to solve the error by adding toArray function and installing the createFragment addon, but it didn't help: 我还尝试通过添加toArray函数并安装createFragment插件来解决该错误,但没有帮助:

function toArray(obj) {
   var array = [];
   for (var i = obj.length >>> 0; i--;) { 
   console.log(obj[i].value)
   array[i] = createFragment(obj[i]);
   }
  return array;
} 

This is how i am trying to render it and it's also the place that yields above shown error: 这就是我试图渲染它的方式,它也是产生上面显示的错误的地方:

                    <select 
                        value={this.state.value} 
                        className="form-control" 
                        id={this.props.id} 
                        ref={this.props.id} 
                        onChange={this.handleChange}>

                        {   
                            tz_options.map(function (item) {
                                x+=1;
                                if (item.value) {
                                    item.key = item.value;
                                } else {
                                    item.key = String(x);
                                }
                                    return item;                                   
                            })
                        }
                    </select>

You should render your select using option elements to represent its children: 你应该使你的select使用option元素来代表它的孩子:

<select 
    value={this.state.value} 
    className="form-control" 
    id={this.props.id} 
    ref={this.props.id} 
    onChange={this.handleChange}>

    {   
        tz_options.map(function (item) {
            x+=1;
            var key;
            if (item.value) {
                key = item.value;
            } else {
                key = String(x);
            }
            return <option key={key} value={item.value}>{item.key}</option>;                                   
        })
    }
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM