user1050619 user1050619 - 6 months ago 13
Javascript Question

reactjs - creating separate modules

Here is my simple reactjs application. It all works fine but I would like to know how to create separate modules ie;I want to separate the components in script.js and add it another file and include it.

How can I do it in reactjs?

Currently, the application is not deployed on any server.

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>

Answer

You can flat out copy CreatePanel and FilterPanel components to different files and include them before myscript.js. It should look something like this:

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