使用React.js检查属性是否存在 [英] Check if property exists using React.js

查看:807
本文介绍了使用React.js检查属性是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用React.js的新手,正在尝试编写一个可重用的组件,该组件具有传递给它的可选属性.在组件中,该可选属性使用流星从数据库中提取数据,然后我要检查返回的对象上是否存在属性(任务上存在parent_task),如果存在,则添加链接.这似乎很简单,但我不断出错.有人对我可能会缺少的东西有什么建议吗?我是否缺少jsx陷阱?

I'm new to using react.js, and am trying to write a re-usable component that has an optional property passed to it. In the component, that optional property pulls data from a db using meteor, then I want to check if a property exists on the returned object (parent_task exists on task), and if exists, adds a link. This seems fairly simple, but I keep getting errors. Does anyone have any suggestions on what I might be missing? Is there a jsx gotcha that I'm missing?

<Header task={params.task_id} />  // rendering component with property

// Task List Header
Header = React.createClass({
  mixins: [ReactMeteorData],

  getMeteorData() {
    var handle = Meteor.subscribe('tasks');

    return {
      taskLoading: ! handle.ready(),
      task: Tasks.findOne({_id: this.props.task})
    }
  },

  getParentTaskLink() {
    if (!this.data.taskLoading) {
      var current_task = this.data.task;

      if (parent_task in current_task) {  // or current_task.hasOwnProperty(parent_task)
        console.log("parent_task exists!");
      }
    }
  },

  render() {
    return (
      <div className="bar bar-header bar-calm">
        {this.getParentTaskLink()} // eventually return anchor element here
        <h1 className="title">Hello World</h1>
      </div>
    )
  }
});

推荐答案

所讨论的道具是什么?怎么样

what is the prop in question? how about

{this.props.propInQuestion ? <a href="#">link</a> : null}

这篇关于使用React.js检查属性是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆