简体   繁体   English

反应嵌套组件错误的道具功能onClick

[英]React nested component wrong props function onClick

I have a little issue using nested components in React. 我在React中使用嵌套组件有一个小问题。 First, I will explain what's the problem. 首先,我将说明问题所在。

I have a survey which contain questions, which contains sub-questions, which contains sub-questions 我有一个调查,其中包含问题,其中包含子问题,其中包含子问题

So we have a survey on 3 levels of questions. 因此,我们对3个问题级别进行了调查。

I'm using the same component called Question. 我正在使用称为Question的相同组件。 If a question has sub question, I call the component Subquestion in which I have again an array of Question. 如果问题有子问题,则将其称为子问题组件,其中又有一个问题数组。

Every Question has function in props to edit or delete them. 每个问题在道具中都有功能来编辑或删除它们。

The problem is that it's working for the questions on level 0 and 1, but not for the last level. 问题在于它适用于0和1级问题,但不适用于最后一级。 Despite of returning the question_id of the level 2 question, it returns the question_id of the level 1 question. 尽管返回了第二级问题的question_id,但它仍返回了第一级问题的question_id。 I guess that it's because of the using of nested components. 我猜这是因为使用了嵌套组件。

Here are the 2 components: 这是2个组件:

Question

class Question extends Component {
  render() {
    const {
      question, onDelete, onEdit, onAddQuestion,
    } = this.props;
    return (
      <div
        key={question.id}
        id={`question-${question.id}`}
        className="panel-item"
      >
        <div className="panel-item-order">
          <div className="form-group form-group-transparent">
            <input type="text" name="input-text-title" className="form-control" placeholder="1" />
          </div>
        </div>
        <div className="panel-item-title">
          { question.name }
        </div>
        <div className="panel-item-mainstay">
          <i className={`icon-${question.pillar.code}`} />
          { question.pillar.name }
        </div>
        <div className="panel-item-actions">
          <button
            className="btn btn-circle"
            onClick={() => onDelete(question.id)}
          >
            <i className="icon-remove-question" />
          </button>
          <button
            className="btn btn-circle"
            onClick={() => onEdit(question.id)}
          >
            <i className="icon-edit" />
          </button>
          {
            question.level < 3 &&
              <div>
                {question.id}
            <button
              className="btn btn-circle"
              onClick={() => onAddQuestion(question.id)}
            >
              <i className="icon-add-question" />
            </button>
              </div>
          }
        </div>
        {
          question.questionsByAnswer &&
            <div className="panel-item-sub">
              { question.questionsByAnswer.map(answer => (
                <div key={answer.id}>
                <SubQuestion
                  answer={answer}
                  onEdit={onEdit}
                  onDelete={onDelete}
                  onAddQuestion={onAddQuestion}
                />
                </div>
                ))
              }
            </div>
        }
        <div className="hori-border-dotted" />
      </div>
    );
  }
}

And here is the SubQuestion 这里是SubQuestion

class SubQuestion extends Component {
  constructor(props) {
    super(props);
    this.state = {
      open: false,
    };

    this.subQuestionsList = this.subQuestionsList.bind(this);
  }

  subQuestionsList(question) {
    return (<Question
      key={question.id}
      question={question}
      onDelete={id => this.props.onDelete(question.id)}
      onEdit={id => this.props.onEdit(question.id)}
      onAddQuestion={this.props.onAddQuestion}
    />);
  }

  render() {
    const { answer } = this.props;
    return (
      <section className="panel-section">
        <div
          className={`title-main title-section panel-section-title ${(this.state.open) ? '' : 'collapsed'}`}
          onClick={() => this.setState({ open: !this.state.open })}
        >
          { answer.label }
          <i className="icon-arrow-bottom" />
        </div>

        <Collapse in={this.state.open}>
          <div id="panel-section-appearance" className="panel-section-content">
            {
              answer.linkedParentQuestions.map(question => (
                this.subQuestionsList(question)
              ))
            }
          </div>
        </Collapse>
      </section>
    );
  }
}

So if we have something like : 所以,如果我们有这样的事情:

Question 1 问题1

--- Question 2 - - 问题2

------- Question 3 -------问题3

If I click on the delete btn on the question 3, it returns the question 2. But if I click on the question 2, it's working (the level 1 too). 如果单击问题3上的delete btn,它将返回问题2。但是,如果单击问题2,它将起作用(级别1也是如此)。

I don't know how I can fix this :( .. 我不知道该如何解决:( ..

Let me know I'm not enough understandable, I will try be more specific 让我知道我还不太了解,我会尝试更具体

Thanks ! 谢谢 !

Ok, found it. 好,找到了。

In SubQuestion component, the following lines: 在SubQuestion组件中,以下几行:

onDelete={id => this.props.onDelete(question.id)} onEdit={id => this.props.onEdit(question.id)} onDelete = {id => this.props.onDelete(question.id)} onEdit = {id => this.props.onEdit(question.id)}

must be : 一定是 :

onDelete={this.props.onDelete)} onEdit={this.props.onEdit)} onDelete = {this.props.onDelete)} onEdit = {this.props.onEdit)}

We do not have to trigger them again. 我们不必再次触发它们。 I got the parent ID because it was triggered every time. 我得到了父ID,因为它每次都会触发。

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

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