【问题标题】:add data target attribute to the button element after clicking点击后为按钮元素添加数据目标属性
【发布时间】:2019-03-06 04:54:41
【问题描述】:

我是react js 的新手。在这里,我确实有一个模态,就像 ->

ErrorComponent.js

import React from 'react';


export default class ErrorComponent extends React.Component {


    constructor(props) {
        super(props);
    }

    render() {
        return (
            <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                <div class="modal-dialog modal-dialog-centered" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="exampleModalCenterTitle">Modal title</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body">
                            ...
      </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                            <button type="button" class="btn btn-primary">Save changes</button>
                        </div>
                    </div>
                </div>
            </div>
        )
    }
}





**LowLeveLCriteria.js**

   class LowLevelCriteria extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            technologies: this.props.techData,
            lowData: this.props.lowData.low,
            showModal: false
        }
    }

 validate (v1, v2) {  return "1" }


    onaddRow(e, id) {
            const errors = this.validate("value1", "value2");

            if (errors === "1") {
            } else {
                this.setState({
            showModal: true
        })
               }


        render() {
        return (
            <div>
                <div className="questionLevelIndication">
                    <span className="levelIndicatorBtn backgroundColorForLow">
                        1
                    </span>
                    <label className="levelIndicationLabel">
                        Low Difficulty Level - Maximum 6 questions
                    </label>
                </div>
                {(this.props.lowData) && this.props.lowData.Low.length > 0 && this.props.lowData.Low.map(data => (
                    <LowRow technologies={this.state.technologies} onChange={this.onchange.bind(this)} data={data} key={data.id} onAddRow={this.onaddRow.bind(this)} onRemoveRow={this.onRemoveRow.bind(this)} />
                ))}
                {this.state.showModal && <ErrorComponent />}
            </div>

        )
    }

<button type="button" className="btn btn-primary btn-sm standard-btn-50 margin-left-10" onClick={(e) => { props.onAddRow(e, props.data.id) }}>+</button>

现在,在这里,当我单击作为子元素的按钮,然后在父组件中调用 AddRow 的方法时。现在,在这里,我想做的是,

如果单击该按钮后,我在 add 函数中调用了另一个函数,该函数验证然后返回一些东西。如果有一个值为“1”,那么我想向用户显示该模式。所以,

我没有办法。我怎样才能做到这一点 ?因为要打开一个模式,我们需要一个数据目标,它需要在按钮上,但我的按钮在另一个组件中。所以,

我尝试使用 document.getElementById,然后在单击按钮后添加数据目标属性。但没有运气。谁能给我一点提示?

【问题讨论】:

  • 抱歉,您的问题似乎不清楚。您是否尝试使用 react 显示对话框但失败了?还是有具体问题(添加数据目标?)?
  • 我的意思是我最初没有在按钮上添加 data-target 属性。但是当用户单击该按钮并且如果某些条件失败时,我想向用户显示一个对话框。但是对于对话框,我需要在按钮本身上有 data target 属性。在某些条件失败后如何添加该属性,然后显示该模型。
  • 我想你正在寻找 boostrap 模态框,这里是使用 react boostrap 的简单模态框链接。 codepen.io/nsieber/pen/grGpzW
  • 与您的代码类似的另一种方式是:jsfiddle.net/sqfhkdcy。希望这两个解决方案对您有所帮助。

标签: javascript html reactjs react-redux


【解决方案1】:

您尝试渲染的所有子组件都应该在组件的render() 方法中。

如果我没记错的话,你正试图从你的处理程序中渲染&lt;ErrorComponent /&gt;,所以它不会工作。如果您尝试使用函数声明创建有状态组件,请参阅参考链接 1 的第一部分。

我建议您的一种方法是:

  • 在父组件中维护一个状态dataTarget,并在按钮处理程序中将其更改为true
  • render() 内有条件地渲染&lt;ErrorComponent /&gt;

dataTarget 的状态在handler 内部改变时,会导致组件重新渲染,ErrorComponent 会显示出来。

请参考这里:

  1. https://reactjs.org/docs/conditional-rendering.html
  2. https://www.robinwieruch.de/conditional-rendering-react/

【讨论】:

  • 对不起,我还没收到这个
  • 你能给我一个JS小提琴吗?
猜你喜欢
  • 2019-05-19
  • 2017-06-08
  • 1970-01-01
  • 2012-10-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多