从另一个文件连接reactjs组件

我从Rails和React开发测试项目。

我有反应的根组件:

# chat_app.js.jsx.coffee ###* @jsx React.DOM ### @ChatApp = React.createClass displayName: 'chatApp' render: -> return ( `

React Chat App Example

` ) React.renderComponent( window.ChatApp(null), document.getElementById('chatApp') )

我有另一个组成部分:

 # message_list.js.jsx.coffee ###* @jsx React.DOM ### @MessageList = React.createClass displayName: 'messageList' render: -> return ( `
qwe
` )

但在浏览器中我有:Uncaught ReferenceError:未定义MessageList

如何将MessageList连接到ChatApp?

使用Rails,您可以将所有JSX文件放在app/assets/javascripts内的components文件夹中,并创建另一个文件app/assets/javascripts/components.js

 //= require_tree ./components 

确保您的application.js文件有//= require_tree . (或添加//= require components ),您将拥有:

  • 从应用程序布局中的默认javascript include标记中为您加载的所有组件
  • 可以在服务页面之前在服务器上预呈现组件

我刚刚在这篇博文中详细描述了这个过程: 使用Ruby on Rails的Isomorphic React应用程序