49

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>
    )
  }
});
3
  • Where are you having the problem? In getParentTaskLink? Commented Nov 17, 2015 at 16:02
  • Not really, why would data.task be empty inside of a data.taskLoading false check? If it was why wouldn't you just do if (!this.data.taskLoading && this.data.task) { and then you know you can use it inside? Commented Nov 17, 2015 at 16:32
  • Props is a JS object so you can use JS functions to check if object contains ... check dmitripavlutin.com/check-if-object-has-property-javascript Commented Nov 18, 2021 at 14:53

9 Answers 9

52

what is the prop in question? how about

{this.props.propInQuestion ? <a href="#">link</a> : null}
Sign up to request clarification or add additional context in comments.

6 Comments

I'm wanting to see if the property parent_task exists on the data object returned (task). I edited the question to make it clearer. I tried your suggestion, but it keeps giving me an error.
@meta-meta isn't it easier to write {this.props.propInQuestion && <a href="#">link</a>}?
Either or. Some devs get confused by the && guard. I could go either way.
I should point out when using this {this.props.propInQuestion && <a href="#">link</a>} make sure this.props.propInQuestion is a bool or coerce it to one. If this.props.propInQuestion is 0, this will render 0.
If this.props.propInQuestion is a present parameter and is a boolean, then you will be induced in error when it comes with a "false" value. I have used this.props.propInQuestion != null and had the desired outcome. @meta-meta I am not sure that you wanted to say the same
|
15

I figured this out. Apparently it was a syntax issue - you need to use a string when searching for properties in objects. The line below works:

if ('parent_task' in current_task)

Comments

11

For me works:

if ('myProperty' in this.props) {}

or

if (this.props.myProperty !== undefined) {}

or

if (this.props.hasOwnProperty('myProperty')) {}

Next condition will not work for number property, as 0 value will not work (such as for empty string):

if (this.props.MaxValue) {}

1 Comment

In my view, this answer is better than the accepted one. A property might exist and its value be false (or 'falsy'), in which case if you check with the ternary operator the result will be wrong if what you intend to ask, as the tile suggests, whether the property exists or not.
4

This works for me

if(this.props.test === undefined){
    console.log('props.test is not defined')
}

Comments

4

Check if a property exists using React.js

There are two options you can use. the && operator and If statement to check if the props exist. Option 1 will check if the property exists then run the second part of the code. It works like an if without the if.

Option 1

this.props.property && this.props.property

Option 2

if(this.props.property){
  this.props.property
}

This also works with function names.

You can use this also check to render components and tags.

2 Comments

Please elaborate your answer to help others arriving at this question.
If the value you expect is a boolean, then it will not work as expected if it comes with "false" value.
1
if(props.hasOwnProperty('propertyName')){
  //do something
} else {
  //do something else
}

1 Comment

This is the best way to check if the property is there or not. Becayse we could have <Component myProp={null} /> and every other kind of check will fail
0

I suggest to try this elegant solution to check callback property on your component:

if(typeof this.props.onClickCallback === 'function') { 
// Do stuff; 
}

or applying destructuring:

const { onClickCallback } = this.props;
if(typeof onClickCallback === 'function') { 
// Do stuff; 
}

Comments

-1

The most upvoted answer

props.propInQuestion ? 'a' : 'b'

Doesn't work if the prop is a boolean and you're trying to check for existence.

Based on How do I check if an object has a key in JavaScript? the fastest way is props.hasOwnProperty('propInQuestion'), with the caveat that this will not search the prototype chain.

Comments

-2

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

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

7 Comments

Sorry, I just left the console.log in there for debugging. The error is saying parent_task is not defined.
Have you tried just checking if(current_task[parent_task]), this should give you the value of the parent_task property in current_task.
That seems to give me the same error. I know in some cases it won't be defined, which is why I want to check. I've never had this issue before with JavaScript in other applications, which makes me think it's a JSX thing.
It's saying parent_task is not defined (because it's not), but that's why I want to check.
Whoops sorry you're right. What I should have typed is if(current_task.parent_task). Using [parent_task] does indeed make it think that parent_task is a variable instead of an attribute. I have edited my original answer to reflect this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.