2014-09-26 22 views
35

Tôi có đơn giản sau đây phản ứng mã trong tập tin JSX tôi:Phản ứng vấn đề thẻ img với url và lớp

/** @jsx React.DOM */ 

var Hello = React.createClass({ 
    render: function() { 
     return <div><img src='http://placehold.it/400x20&text=slide1' alt={event.title} class="img-responsive"/><span>Hello {this.props.name}</span></div>; 
    } 
}); 

React.renderComponent(<Hello name="World" />, document.body); 

Sản lượng trong DOM được như sau:

<div data-reactid=".0"> 
    <img src="http://placehold.it/400x20undefined1" data-reactid=".0.0"> 
    <span data-reactid=".0.1"> 
    <span data-reactid=".0.1.0">Hello </span> 
    <span data-reactid=".0.1.1">World</span> 
    </span> 
</div> 

Tôi có hai vấn đề với nó:

Bất kỳ ý tưởng nào?

Trả lời

50

Hãy nhớ rằng img của bạn không thực sự là phần tử DOM mà là biểu thức javascript.

  1. Đây là biểu thức thuộc tính JSX. Đặt dấu ngoặc nhọn xung quanh biểu thức chuỗi src và nó sẽ hoạt động. Xem http://facebook.github.io/react/docs/jsx-in-depth.html#attribute-expressions

  2. Trong javascript, thuộc tính lớp là tham chiếu sử dụng className. Xem các lưu ý trong phần này: http://facebook.github.io/react/docs/jsx-in-depth.html#react-composite-components

    /** @jsx React.DOM */ 
    
    var Hello = React.createClass({ 
        render: function() { 
         return <div><img src={'http://placehold.it/400x20&text=slide1'} alt="boohoo" className="img-responsive"/><span>Hello {this.props.name}</span></div>; 
        } 
    }); 
    
    React.renderComponent(<Hello name="World" />, document.body); 
    
+2

Và nếu bạn kiểm tra đầu ra, bạn có thể nhận ra rằng các thuộc tính alt cũng là mất tích. :( – ruff

+1

alt phải giống như 'alt = {" boohoo "}'. –

0
var Hello = React.createClass({ 
    render: function() { 
     return (
     <div className="divClass"> 
      <img src={this.props.url} alt={`${this.props.title}'s picture`} className="img-responsive" /> 
      <span>Hello {this.props.name}</span> 
     </div> 
    ); 
    } 
}); 
Các vấn đề liên quan