reactjs - 创建单独的模块

时间:2016-05-13 17:20:36

标签: javascript reactjs

这是我简单的reactjs应用程序。一切正常,但我想知道如何创建单独的模块,即;我想分离script.js中的组件,并将其添加到另一个文件并包含它。

我怎么能在reactjs中做到这一点?

目前,该应用程序未部署在任何服务器上。

的script.js:

var CreatePanel = React.createClass({ 
    render: function(){ 
        return <div className="row">
            <div className = "col-xs-6 col-sm-3">
                <input type="text"></input>
                <select></select>
            </div>
        </div>; 
    } 
}); 

var FilterPanel = React.createClass({ 
    render: function(){ 
        return <div className="row">Filter Panel</div>; 
    } 
}); 

React.render(<div class="container">
    <CreatePanel/>   
    <FilterPanel/> 
</div>
    , document.getElementById('react-container'));

HTML

<!DOCTYPE html>
<html>
<head>
    <script src="fb.me/react-0.13.3.min.js"></script>
    <script src="cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.29/browser.js"></script>

    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
    <title>React Components</title>
</head>

<body>
    <div id="react-container"></div>
    <script type="text/babel" src="./myscript.js">
    </script>
</body>

</html>

2 个答案:

答案 0 :(得分:2)

您可以将CreatePanelFilterPanel组件复制到不同的文件,并在myscript.js之前包含它们。看起来应该是这样的:

createpanel.js

var CreatePanel = React.createClass({ 
    render: function(){ 
        return <div className="row">
            <div className = "col-xs-6 col-sm-3">
                <input type="text"></input>
                <select></select>
            </div>
        </div>; 
    } 
}); 

filterpanel.js

var FilterPanel = React.createClass({ 
    render: function(){ 
        return <div className="row">Filter Panel</div>; 
    } 
}); 

myscript.js

React.render(<div class="container">
    <CreatePanel/>   
    <FilterPanel/> 
</div>
    , document.getElementById('react-container'));

的index.html

<!DOCTYPE html>
<html>
<head>
    <script src="fb.me/react-0.13.3.min.js"></script>
    <script src="cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.29/browser.js"></script>

    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
    <title>React Components</title>
</head>

<body>
    <div id="react-container"></div>
    <script type="text/babel" src="./createpanel.js">
    </script>
    <script type="text/babel" src="./filterpanel.js">
    </script>
    <script type="text/babel" src="./myscript.js">
    </script>
</body>

</html>

答案 1 :(得分:2)

一种常见的方法是使用ES6模块和React组件的类语法。您将再次将您的组件分离到单个文件和出口他们:

CreatePanel.jsx

import React from 'react';    

export default class CreatePanel extends React.Component { 
  render() { 
    return 
      (<div className="row">
        <div className = "col-xs-6 col-sm-3">
          <input type="text"></input>
            <select></select>
        </div>
      </div>); 
  } 
}; 

FilterPanel.jsx

import React from 'react';    

export default class FilterPanel extends React.Component { 
  render() { 
    return <div className="row">Filter Panel</div>; 
  } 
});

然后,您可以将这些组件导入到运行React.render()的script.js中

的script.js

import React from 'react';

import CreatePanel from 'your relative path to CreatePanel.jsx';
import FilterPanel from 'your relative path to FilterPanel.jsx';

React.render(<div class="container">
<CreatePanel/>   
<FilterPanel/> 
</div>
, document.getElementById('react-container'));