简体   繁体   English

ReactJS:在渲染中重用 div 元素 function

[英]ReactJS: Reusing div element inside render function

Updated My Question based on comments.根据评论更新了我的问题。 Not able to make small snippets as its a legacy code with lot of interacting components and I am very new to React to understand all the components.无法将小片段作为具有许多交互组件的遗留代码制作,我对 React 非常陌生,无法理解所有组件。

I have the following GUI:我有以下图形用户界面: 在此处输入图像描述

If you see carefully I have multiple fields with corresponding dropdowns.如果您仔细看,我有多个带有相应下拉列表的字段。 This combination is represented within a Div in my code under a class within render() function.这种组合在我的代码中的一个 Div 中表示,位于 render() function 中的 class 下。

The problem is the Div code is repeatedly copy-paste to create a new field and dropdowns.问题是 Div 代码反复复制粘贴以创建新字段和下拉菜单。 I don't want to repeat the whole div again and again and want to replace it with some variable or function which accepts Field name and DROP_DOWN_TYPE.我不想一次又一次地重复整个 div 并想用一些变量或 function 来替换它,它接受字段名称和 DROP_DOWN_TYPE。

I have tried many solutions but it resulted in various errors due to onchange event.我尝试了很多解决方案,但由于 onchange 事件导致了各种错误。

Repeated div is shown below:重复的 div 如下所示:

<div className="infoBlock">
    <div className="row align-items-center">
        <div className="col-6">
            <div className="row">
                <div className="col-4">
                    <label className="b_label">Category :</label>
                </div>
                <div className="col-8">
                    <Dropdown type="Categories" isMultiple={true} onchange={this.handleDDClick} />
                </div>
            </div>
        </div>
        <div className="col-6">

        </div>
    </div>
</div>

I can't show DropDown code because its a huge file.我无法显示 DropDown 代码,因为它是一个巨大的文件。 But its a normal drop down which makes a service call to populate its fields.但它是一个正常的下拉菜单,它会调用服务来填充其字段。

Can you give more clarity by making a demo kind of thing on codesandbox.io?你能通过在codesandbox.io上做一个演示来更清楚吗?

What I feel is you are repeatedly calling the setState function according to the error.我的感觉是您根据错误反复调用setState function。

Either your state is being called repeatedly somewhere, or you need to bind that this.handleDDClick onto that onchange in Dropdown tag.您的 state 在某处被重复调用,或者您需要将this.handleDDClick绑定到Dropdown标记中的onchange上。 Sometimes it gets triggered without actually any change happening in the dropdown.有时它会被触发,而下拉菜单中实际上没有发生任何变化。

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

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