2016-07-11 49 views
2

我试图从React中的父组件访问信息,但不断收到错误。 React似乎无法理解imageUrl和用户名是另一个组件的一部分。请帮忙!React父子组件不起作用

这里是我的代码:

var USER_DATA = { 
 
\t name: 'Ayaz Uddin', 
 
\t username: 'ayaz2589', 
 
\t image: 'https://scontent-lga3-1.xx.fbcdn.net/v/t1.0-9/1511676_10152124607269164_1288963176_n.jpg?oh=d725f8677a8369269e2ea65ffe9a3a63&oe=582CDD2C' 
 
} 
 

 
var React = require('react'); 
 
var ReactDOM = require('react-dom'); 
 

 
var ProfilePic = React.createClass({ 
 
\t render: function() { 
 
\t \t return <img src={this.props.imageUrl} style={{ height: 100, width: 100}} /> 
 
\t } 
 
}); 
 

 
var ProfileLink = React.createClass({ 
 
\t render: function() { 
 
\t \t return (
 
\t \t \t <div> 
 
\t \t \t \t <a href={'http://www.github.com/' + this.props.username}> 
 
\t \t \t \t \t {this.props.username} 
 
\t \t \t \t </a> 
 
\t \t \t </div> 
 
\t \t) 
 
\t } 
 
}); 
 

 
var ProfileName = React.createClass({ 
 
\t render: function() { 
 
\t \t return (
 
\t \t \t <div>{this.props.name}</div> 
 
\t \t) 
 
\t } 
 
}); 
 

 
var Avatar = React.createClass({ 
 
\t render: function() { 
 
\t \t return (
 
\t \t \t <div> 
 
\t \t \t \t <profilePic imageUrl={this.props.user.image} /> 
 
\t \t \t \t <profileName name={this.props.user.name} /> 
 
\t \t \t \t <profileLink username={this.props.user.username} /> 
 
\t \t \t </div> 
 
\t \t) 
 
\t } 
 
}) 
 

 
ReactDOM.render(
 
\t <Avatar user={USER_DATA} />, 
 
\t document.getElementById('app') 
 
);

,这里是错误:

enter image description here

+0

类名有所不同,你打电话profilePic但定义的类是ProfilePic,是一个错字? –

回答

1

变化:

<profilePic imageUrl={this.props.user.image} /> 
<profileName name={this.props.user.name} /> 
<profileLink username={this.props.user.username} /> 

要:

<ProfilePic imageUrl={this.props.user.image} /> 
<ProfileName name={this.props.user.name} /> 
<ProfileLink username={this.props.user.username} /> 

这里是fiddle与所做的更改。

+0

谢谢!有效! –

相关问题