简体   繁体   English

如何在 Reactjs 中通过 onClick 扩展 div 元素

[英]How to extend div element through onClick in Reactjs

I'm new to Reactjs and have more experience with regular html/js but need to use Reactjs for a project I am working on.我是 Reactjs 的新手,对常规 html/js 有更多经验,但需要在我正在处理的项目中使用 Reactjs。

I want to allow users to click on a button if they want to add another setting.如果他们想添加另一个设置,我想允许用户单击一个按钮。 I need help in implementing a button that when clicked, extends the existing div and displays 2 more options to users (Metric and Threshold input boxes).我需要帮助来实现一个按钮,当单击该按钮时,它会扩展现有的 div 并向用户显示另外 2 个选项(度量和阈值输入框)。 ie if the button is clicked 3 times, there will be 6 input boxes appearing on the page.即如果按钮被点击3次,页面上会出现6个输入框。

Currently once the button is clicked, the function call is working as addItem() is getting triggered.目前,一旦单击按钮,function 调用就会在触发 addItem() 时工作。 However, I'm not sure how to implement code to display more options to users.但是,我不确定如何实现代码以向用户显示更多选项。

My code so far:到目前为止我的代码:

addItem() {
     
};

...

return (
...
<Form.Item name="frequency" label="Metric">
</Form.Item>
<Form.Item name="amount" label="Threshold Score">
   <Slider marks={marks} />
   <button onClick={this.addItem()} {...formItemLayout} labelAlign="right" type="submit" className="btn 
    btn-success px-5" >
       Add Metric
   </button>    
</Form.Item>
)

This is a snapshot of the webpage, if a user clicks on 'Add Metric' then the page should be extended and another input box of Metric and Threshold Score should appear.这是网页的快照,如果用户单击“添加指标”,则应扩展页面并出现另一个指标和阈值分数输入框。

这是网页的快照,如果用户单击“添加指标”,则应扩展页面并出现另一个指标和阈值分数输入框。

You are direct call the function in the onClick event.您可以在 onClick 事件中直接调用 function。 that's why it's didn't work.这就是为什么它不起作用。 You can two ways to call the function in the event.您可以通过两种方式在事件中调用 function。 For example.例如。 onClick={this.addItem} or onClick={()=>this.addItem()} onClick={this.addItem}onClick={()=>this.addItem()}

You can trigger an event in 2 ways.您可以通过 2 种方式触发事件。 You need to call a direct function on the onclick event.您需要在 onclick 事件上直接调用 function。 first onClick={this.addItem} and seconf one is onClick={(event) => this.addItem(event)} .第一个onClick={this.addItem}和 seconf 一个是onClick={(event) => this.addItem(event)}

class Demo extends Component {
    constructor(props) {
        super(props);
        this.state = {}
    }
        
    addItem() {
        console.log('trigger);
    };
    
    render() {
            return (
                <Form.Item name="frequency" label="Metric">
                </Form.Item>
                <Form.Item name="amount" label="Threshold Score">
                     <Slider marks={marks} />
                     <button onClick={this.addItem} {...formItemLayout} labelAlign="right" type="submit" className="btn 
                        btn-success px-5" >
                             Add Metric
                     </button>    
                </Form.Item>
                )
        }
} 

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

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