2014-09-26 17 views
35

Ho il seguente semplice reagire codice nel mio file JSX:Reagire problema tag img con url e la classe

/** @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); 

L'uscita nel DOM è la seguente:

<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> 

ho due problemi con esso:

Qualche idea?

risposta

50

Ricorda che il tuo img non è realmente un elemento DOM ma un'espressione javascript.

  1. Questa è un'espressione di attributo JSX. Metti le parentesi graffe attorno all'espressione della stringa src e funzionerà. Vedi http://facebook.github.io/react/docs/jsx-in-depth.html#attribute-expressions

  2. In javascript, l'attributo di classe fa riferimento utilizzando className. Vedere la nota in questa sezione: 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

E se si controlla l'output, è possibile rendersi conto che l'attributo alt è anche mancante. :( – ruff

+1

L'alt dovrebbe essere come '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> 
    ); 
    } 
}); 
Problemi correlati