最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Component undefined in ReactJS - Stack Overflow

programmeradmin3浏览0评论

I'm playing around with ReactJS. I have defined three ponents, which are nested:

UserProfile.jsx

var React = require('react');

var UserProfile = React.createClass({

  getInitialState: function() {
    return {
      username: "zuck"
    };
  },

  render: function() {
    return (
      <UserProfile>
        <ProfileImage username={this.props.username}/>
        <ProfileLink username={this.props.username}/>
      </UserProfile>
    );
  }

});
React.render(<UserProfile username="zuck"/>, document.body);

module.exports = UserProfile;

ProfileLink.jsx

var React = require('react');

var ProfileLink = React.createClass({

  render: function() {
    return (
      <a href="//facebook/{this.props.username}">{this.props.username}</a>
    );
  }

});

module.exports = ProfileLink;

ProfileImage.jsx

var React = require('react');

var ProfileImage = React.createClass({

  render: function() {
    return (
      <img src="//graph.facebook/{this.props.username}/picture"/>
    );
  }

});

module.exports = ProfileImage;

My html file basically only includes the three jsx files (btw, is there a way to bundle all these into a single request during development?)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>React FB Link</title>
</head>
<body>
  <script type="text/javascript" src="UserProfile.jsx"></script>
  <script type="text/javascript" src="ProfileLink.jsx"></script>
  <script type="text/javascript" src="ProfileImage.jsx"></script>
</body>
</html>

I'm using beefy to handle and serve the JSX files, using beefy *.jsx 8000 -- -t reactify.

The resulting files are (in truncated form):

  • UserProfile.jsx
  • ProfileLink.jsx
  • ProfileImage.jsx

Loading the html page results in an error:

Uncaught ReferenceError: ProfileImage is not defined with reference to line 15 in UserProfile.jsx:

React.createElement(ProfileImage, {username: this.props.username}),

I'm playing around with ReactJS. I have defined three ponents, which are nested:

UserProfile.jsx

var React = require('react');

var UserProfile = React.createClass({

  getInitialState: function() {
    return {
      username: "zuck"
    };
  },

  render: function() {
    return (
      <UserProfile>
        <ProfileImage username={this.props.username}/>
        <ProfileLink username={this.props.username}/>
      </UserProfile>
    );
  }

});
React.render(<UserProfile username="zuck"/>, document.body);

module.exports = UserProfile;

ProfileLink.jsx

var React = require('react');

var ProfileLink = React.createClass({

  render: function() {
    return (
      <a href="//facebook./{this.props.username}">{this.props.username}</a>
    );
  }

});

module.exports = ProfileLink;

ProfileImage.jsx

var React = require('react');

var ProfileImage = React.createClass({

  render: function() {
    return (
      <img src="//graph.facebook./{this.props.username}/picture"/>
    );
  }

});

module.exports = ProfileImage;

My html file basically only includes the three jsx files (btw, is there a way to bundle all these into a single request during development?)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>React FB Link</title>
</head>
<body>
  <script type="text/javascript" src="UserProfile.jsx"></script>
  <script type="text/javascript" src="ProfileLink.jsx"></script>
  <script type="text/javascript" src="ProfileImage.jsx"></script>
</body>
</html>

I'm using beefy to handle and serve the JSX files, using beefy *.jsx 8000 -- -t reactify.

The resulting files are (in truncated form):

  • UserProfile.jsx
  • ProfileLink.jsx
  • ProfileImage.jsx

Loading the html page results in an error:

Uncaught ReferenceError: ProfileImage is not defined with reference to line 15 in UserProfile.jsx:

React.createElement(ProfileImage, {username: this.props.username}),
Share Improve this question asked Mar 30, 2015 at 9:28 doquedoque 2,7337 gold badges29 silver badges47 bronze badges 1
  • 1 I would use a bundler and then use var ProfileImage = require('ProfileImage'). – magnudae Commented Mar 30, 2015 at 10:00
Add a ment  | 

1 Answer 1

Reset to default 3

You might need to load ProfileImage.jsx and ProfileLink.jsx before your UserProfile.jsx since right now the page is parsing Userprofile.jsx first and it doesn't know what ProfileImage mean (because you haven't loaded it yet)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>React FB Link</title>
</head>
<body>
  <script type="text/javascript" src="ProfileLink.jsx"></script>
  <script type="text/javascript" src="ProfileImage.jsx"></script>
  <script type="text/javascript" src="UserProfile.jsx"></script>
</body>
</html>

You can use any module bundler to bundle up your files (Browserify, Gulp, Webpack) into one single file as entry point

发布评论

评论列表(0)

  1. 暂无评论