【问题标题】:Conditional rendering of a static partial in react - MERN反应中静态部分的条件渲染 - MERN
【发布时间】:2017-12-28 05:02:38
【问题描述】:

我试图弄清楚如何有条件地渲染部分反应。我的条件是,当列表项处于活动状态时,应该呈现部分。

我正在使用引导程序,它有一个可用的活动元素。

我的目标是实现类似于对讲机的条款页面:https://www.intercom.com/terms-and-policies#billing

在我的主菜单中,我有:

import React from 'react';
import ReactDOM from 'react-dom';

var Terms = require('../overview/terms.jsx');
var Privacy = require('../overview/privacy.jsx');
var Cookies = require('../overview/cookies.jsx');
var Thirdparty = require('../overview/thirdparty.jsx');
var Website = require('../overview/website.jsx');
var Sla = require('../overview/sla.jsx');
var Acceptable = require('../overview/acceptable.jsx');
var Billing = require('../overview/billing.jsx');
var Information = require('../overview/information.jsx');
var Incident = require('../overview/incident.jsx');
var Bug = require('../overview/bug.jsx');


class Menu extends React.Component {

render() {
    return (
      <div>
        <div className = "row">
          <div className = "col-md-8 col-md-offset-2 generalhead">
            Terms and Policies
          </div>
        </div>
        <div className = "row">
          <div className = "col-md-3 col-md-offset-1">
            <ul className="list-group">

              <a href="#" className="list-group-item list-group-item-action listitems" data-attribute = "terms">Terms of Service</a>
              <a href="#" className="list-group-item list-group-item-action listitems" data-attribute = "privacy">Privacy Policy</a>
              <a href="#" className="list-group-item list-group-item-action listitems" data-attribute = "cookies">Cookies</a>
              <a href="#" className="list-group-item list-group-item-action listitems" data-attribute = "thirdparty">Third Party Processors</a>
              <a href="#" className="list-group-item list-group-item-action listitems" data-attribute = "website">Website Use</a>
              <a href="#" className="list-group-item list-group-item-action listitems" data-attribute = "sla">Service Level Agreement</a>
              <a href="#" className="list-group-item list-group-item-action listitems" data-attribute = "acceptable">Acceptable Use</a>
              <a href="#" className="list-group-item list-group-item-action listitems" data-attribute = "billing">Billing</a>
              <a href="#" className="list-group-item list-group-item-action listitems" data-attribute = "information">Information Security</a>
              <a href="#" className="list-group-item list-group-item-action disabled listitems" data-attribute = "incident">Incident Response Plan</a>
              <a href="#" className="list-group-item list-group-item-action disabled listitems" data-attribute = "bug">Bug Bounty</a>

            </ul>
          </div>
          <div className = "col-md-6 col-md-offset-1">
             <Terms />
             <Privacy />
             <Cookies />
             <Thirdparty />
             <Website />
             <Sla />
             <Acceptable />
             <Billing />
             <Information />
             <Incident />
             <Bug />
          </div>

        </div>
      </div>
    );
  }
}

module.exports = Menu;

每个列表项都有一个命名数据属性。当该属性是活动项时,我想渲染部分。目前它们都在渲染。

我已阅读有关条件渲染的 react 文档:https://facebook.github.io/react/docs/conditional-rendering.html

我试图定义一个名为 active 的 const,我认为它可能会利用 bootstrap 类——但这不起作用。对于如何做到这一点,我必须有错误的一端。我似乎是一个用于 rails js 切换的简单隐藏秀。我不知道如何开始在反应中解决这个问题。

谁能指引我正确的方向?

【问题讨论】:

    标签: javascript twitter-bootstrap reactjs conditional


    【解决方案1】:

    跟踪哪个列表项在您的状态中处于活动状态

    <a href="#" onClick={() => this.setState({active: 'terms'})}>Terms of Service</a>
    

    然后有条件地只渲染活动项

    {this.state.active == 'terms' && <Terms />}
    

    但我建议改用 react 路由器,因为每个项目似乎都对应一个页面。

    编辑

    你必须为你的类添加一个构造函数来使它有状态

    class Menu extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                active: false
            };
        }
        .
        .
        .
    

    【讨论】:

    • 感谢您的帮助。我试图使用这个建议。我认为部分问题可能是我使用扩展 React 来创建类而不是“create.class”。我更改了 this 语句以确保“this”绑定 {this.state.active.bind(this) == 'terms' && }。我收到一条错误提示 TypeError: Cannot read property 'active' of null
    • 它与 extend React.Component 无关,这只是意味着您正在扩展 react 提供的 Component 类。这就是你制作类组件的方式。或者你可以extends PureComponent。无需 .bind(this) 到 this.state.active。除非您在那里存储计划用作回调的函数,否则您将在类的构造函数中进行绑定。您将需要一个类的构造函数并使用super,以便您可以访问this,然后设置类的初始状态。阅读此facebook.github.io/react/docs/react-component.html
    • @KyleRichardson - 谢谢 - 如果我尝试使用 FuzzyTree 的建议,我会收到一条错误消息:TypeError: Cannot read property 'active' of null。谷歌搜索该错误提示了绑定语句。能不能看一下概述有相关的解释?
    • 好的,所以我没有提供答案,因为你的问题让我有点困惑。您所有的“列表项”都是锚链接。有什么特别的原因吗?单击它们是否会将您导航到某个地方?当您单击它们时,它们会加载部分吗?或者当有人碰巧在该网址上时,它们是否只是显示为活动的?部分内容是否与您的网址相关?
    • @Mel 正如 Kyle 所说,您需要向您的类添加一个构造函数。查看更新
    猜你喜欢
    • 1970-01-01
    • 2020-06-27
    • 2022-12-10
    • 2022-01-17
    • 2019-03-25
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多