Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if property exists using React.js

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>     )   } }); 
like image 578
bgmaster Avatar asked Nov 17 '15 15:11

bgmaster


People also ask

How do I check if an object contains a property?

We can check if a property exists in the object by checking if property !== undefined . In this example, it would return true because the name property does exist in the developer object.

How do you check if value exists in array React?

To check if an element exists in an array in React: Use the includes() method to check if a primitive exists in an array. Use the some() method to check if an object exists in an array.

How do you check if a property exists in an object TypeScript?

To check if a property exists in an object in TypeScript: Mark the specific property as optional in the object's type. Use a type guard to check if the property exists in the object. If accessing the property in the object does not return a value of undefined , it exists in the object.


1 Answers

what is the prop in question? how about

{this.props.propInQuestion ? <a href="#">link</a> : null} 
like image 89
meta-meta Avatar answered Oct 06 '22 00:10

meta-meta