javascript - 动态显示依赖项

标签 javascript reactjs

我有一个包含父问题和从属问题的表单。 默认情况下需要隐藏相关问题。如果父问题的回答为"is",则应显示从属问题。 从属问题包含父问题 ID。

对象结构如下。

{
  "module_question_id": "e04f487b-4782-11e8-a89b-408d5ce49fe0",
  "question": "Parent question",
  "question_type": "boolean",
  "is_required": 1,
  "dependant_answer_id": null,
  "parent_question_id": null,
  "answers": [
    {
      "module_answer_id": "ec52835f-4782-11e8-a89b-408d5ce49fe0",
      "answer": "Yes"
    },
    {
      "module_answer_id": "f26a2fa0-4782-11e8-a89b-408d5ce49fe0",
      "answer": "No"
    },
    {
      "module_answer_id": "521f5e95-534c-11e8-9c0a-408d5ce49fe0",
      "answer": "I don't know"
    }
  ]
},
{
  "module_question_id": "5a71221b-4795-11e8-a89b-408d5ce49fe0",
  "question": "Dependent question",
  "question_type": "boolean",
  "is_required": 0,
  "dependant_answer_id": "ec52835f-4782-11e8-a89b-408d5ce49fe0",
  "parent_question_id": "e04f487b-4782-11e8-a89b-408d5ce49fe0",
  "answers": [
    {
      "module_answer_id": "66d94086-4795-11e8-a89b-408d5ce49fe0",
      "answer": "Yes"
    },
    {
      "module_answer_id": "6a397737-4795-11e8-a89b-408d5ce49fe0",
      "answer": "No"
    },
    {
      "module_answer_id": "1667b12f-534d-11e8-9c0a-408d5ce49fe0",
      "answer": "I don't know"
    }
  ]
}

我隐藏了页面加载的相关问题,如下所示。

 <div className={this.getWrapperClass()}>
      ...
</div>
  getWrapperClass() {
    let wrapperClsName = this.props.questionInfo.parent_question_id == null ? `wrap-${this.props.questionInfo.question_type}` : `hide wrap-${this.props.questionInfo.question_type}`;
    return wrapperClsName;
  }

现在,当在父问题上选择"is"时,我需要显示从属问题。

知道如何实现这个吗?

最佳答案

在 React 中通常不会使用类来显示和隐藏。相反,您可以根据状态呈现或不呈现问题。

这是一个简单的示例,请参阅片段内的评论:

class Example extends React.Component {
  constructor(...args) {
    super(...args);
    // Initial state: Not showing
    this.state = {
      showingQuestion: false
    };
    
    // Event handler for checkbox
    this.tickBox = event => {
      this.setState({
        showingQuestion: event.currentTarget.checked
      });
    };
  }
  
  render() {
    // Render our UI for whether to show the question, and render
    // the question *if* our state is that we're showing it
    return (
      <div>
        <label>
          Show the question
          <input type="checkbox" onClick={this.tickBox} />
        </label>
        {this.state.showingQuestion &&
          <div>
            This is the question
          </div>
        }
      </div>
    );
  }
}

ReactDOM.render(
  <Example />,
  document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

这是根据状态中的标志有条件地呈现问题的位:

{this.state.showingQuestion &&
  <div>
    This is the question
  </div>
}

关于javascript - 动态显示依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50247302/

相关文章:

javascript - Eloquent 模型属性作为驼峰大小写 [Laravel 5.2] [Dingo API]

javascript - 如何在组件内使用 React 自定义环境变量

javascript - 使用 JQuery 从 html5 视频播放器触发事件

javascript - NextJS动态路由,重新渲染特定的子组件

javascript - 状态零结果谷歌地图地理编码器状态

reactjs - Next.Js 静态站点生成与 headless CMS Assets

javascript - 切换点击选择标签 - React

javascript - 回发后文本框失去值(value)

javascript - 如何在 vuejs 中使用数据初始化应用程序

javascript - 有时会嵌入错误的 svg。在React/Redux/Webpack项目中使用react-inlinesvg