user1050619
user1050619

Reputation: 20856

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>

Upvotes: 0

Views: 4478

Answers (2)

Brad Colthurst
Brad Colthurst

Reputation: 2605

A common approach would be to make use of ES6 modules & class syntax for your React components. You would again separate out your components to individual files & export them:

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>; 
  } 
});

Then you can import those components to your script.js where you are running React.render()

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')); 

Upvotes: 2

frontsideair
frontsideair

Reputation: 528

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>

Upvotes: 2

Related Questions