2017-07-22 38 views
1

我想弄清楚如何有条件地渲染部分反应。我的情况是,当列表项目处于活动状态时,应该呈现部分内容。有条件地渲染静态部分的反应 - MERN

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

我的目标是实现类似对讲机的条款页面的内容: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; 

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

我已阅读上的反应条件呈现文档:https://facebook.github.io/react/docs/conditional-rendering.html

我试图界定一个const称为主动,我认为可能利用引导类的 - 但是,这是行不通的。对于如何做到这一点,我必须得到错误的结局。我看起来像一个简单的隐藏节目为一个轨道js切换。我无法弄清楚如何开始解决这个问题。

任何人都可以给我一个正确的方向指导吗?

回答

1

跟踪哪些列表项目是活跃在您的国家

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

然后有条件地仅使活动项目

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

但我建议使用反应路由器替代,因为每个项目似乎对应页面。

编辑

你有一个构造函数添加到类,使其状态

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

感谢您的帮助。我试图使用这个建议。我认为问题的一部分可能是我使用扩展React来创建类而不是'create.class'。我改变了这个说法,试图确保“this”绑定,{this.state.active.bind(this)=='terms'&& }。我得到一个错误,说TypeError:无法读取null的属性'active' – Mel

+0

它与'扩展React.Component'无关,只是意味着你扩展了可以提供反应的Component类。这是你如何制作类组件。或者你可以'扩展PureComponent'。不需要'将(this)'绑定到this.state.active。除非你在那里存储一个你打算用作回调的函数,但是你会在类的构造函数中进行绑定。你需要在你的类上有一个构造函数,并且使用'super',这样你就可以访问'this',然后设置类的初始状态。阅读这个https://facebook.github.io/react/docs/react-component.html –

+0

@KyleRichardson - 谢谢 - 如果我尝试使用FuzzyTree的建议,我得到一个错误,说:TypeError:无法读取属性'活跃'的空值。谷歌搜索错误提示绑定声明。你能看到一些概述有相关的解释吗? – Mel