javascript - React handle empty props - Stack Overflow

I want to render an image, whose url I fetch from an API on app start. When I do this react crahses wit

I want to render an image, whose url I fetch from an API on app start. When I do this react crahses with following message: TypeError: Cannot read property 'icon' of undefined.
Whilst icon is a property within an object I can access everything else and even the object.

class Current extends React.Component {
  render() {
    console.log(this.props.current.condition);
    // Ok, first I write undefined to the console, but then the object
    console.log(this.props.current.condition.icon);
    // BAM. Doomsday.

    return (
      // Beneath me everything is totaly fine.
      <div className="Current">
        <div className="Important">
          <div>
            <img src={this} alt={this} />
            <span>{this.props.current.temp_c}</span>
          </div>
          <h1>{this.props.location.name}, {this.props.location.country}</h1>
          <p>{this.props.location.localtime}</p>
        </div>
        <h1>hey</h1>
      </div>
    );
  }
}

export default Current;

I tried juggeling the object with ComponentWillMount and ComponentDiDMount but it didn't help. How can I access the icon property without crashing the application?

Edit: Kind of fixed by this:

<img 
src={typeof(this.props.current.condition) === 'undefined' ? '' : this.props.current.condition.icon} 
alt={typeof(this.props.current.condition) === 'undefined' ? '' : this.props.current.condition.text} 
/>

...but this can't be clean code, right?

I want to render an image, whose url I fetch from an API on app start. When I do this react crahses with following message: TypeError: Cannot read property 'icon' of undefined.
Whilst icon is a property within an object I can access everything else and even the object.

class Current extends React.Component {
  render() {
    console.log(this.props.current.condition);
    // Ok, first I write undefined to the console, but then the object
    console.log(this.props.current.condition.icon);
    // BAM. Doomsday.

    return (
      // Beneath me everything is totaly fine.
      <div className="Current">
        <div className="Important">
          <div>
            <img src={this} alt={this} />
            <span>{this.props.current.temp_c}</span>
          </div>
          <h1>{this.props.location.name}, {this.props.location.country}</h1>
          <p>{this.props.location.localtime}</p>
        </div>
        <h1>hey</h1>
      </div>
    );
  }
}

export default Current;

I tried juggeling the object with ComponentWillMount and ComponentDiDMount but it didn't help. How can I access the icon property without crashing the application?

Edit: Kind of fixed by this:

<img 
src={typeof(this.props.current.condition) === 'undefined' ? '' : this.props.current.condition.icon} 
alt={typeof(this.props.current.condition) === 'undefined' ? '' : this.props.current.condition.text} 
/>

...but this can't be clean code, right?

Share Improve this question asked Aug 17, 2018 at 14:06 PeterPeter 1,9242 gold badges35 silver badges60 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3
class Current extends React.Component {
  render() {
    const { current } = this.props
    if ( !(current && current.condition) ) return <span>Loading</span>;

    return (
      // Beneath me everything is totaly fine.
      <div className="Current">
        <div className="Important">
          <div>
            <img src={this} alt={this} />
            <span>{this.props.current.temp_c}</span>
          </div>
          <h1>{this.props.location.name}, {this.props.location.country}</h1>
          <p>{this.props.location.localtime}</p>
        </div>
        <h1>hey</h1>
      </div>
    );
  }
}

export default Current;

try

src={this.props.current.condition && this.props.current.condition.icon}

The correct way to test if a variable is undefined is like this:

this.props.current.condition === undefined

There is no need to use typeof() because undefined is a valid value in JavaScript code.

You can simplify this in a condition because undefined is already considered "falsy". This means that you can use an undefined value directly in an if statement. In React, the mon idiom is like this:

this.props.current.condition && this.props.current.condition.icon

This will evaluate to undefined if this.props.current.condition is undefined. Otherwise, it will evaluate to the value of this.props.current.condition.icon.

For a deeper understanding, I suggest that you learn about "truthiness" and "falsiness" in JavaScript. I also suggest you learn about boolean operators and short-circuiting.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745661043a4638837.html

相关推荐

  • javascript - React handle empty props - Stack Overflow

    I want to render an image, whose url I fetch from an API on app start. When I do this react crahses wit

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信