Check if property exists using React.js

I am new to using response.js and trying to write a reusable component that has an optional property passed to it. In the component, this optional property retrieves data from db using a meteorite, then I want to check if the property exists on the returned object (parent_task exists in the task), and if it exists, it adds a link. It seems pretty simple, but I get errors all the time. Does anyone have any suggestions on what I might lose? Is there any jsx gotcha that I am 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>
    )
  }
});
+4
source share
3 answers

What is a prop? What about

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

. -, - . :

if ('parent_task' in current_task)
+4

You need to return from getParentTaskLink () with the link you need.

 if (current_task.parent_task) {
      return (<a href="#">link</a>);
    } else { return null; }
-1
source

Source: https://habr.com/ru/post/1616241/


All Articles