1

I have built a react component which I would like to render from within a second component in a separate file.

StoryStrap.react.js

var React = require('react');
import { Input, ButtonInput, Alert } from 'react-bootstrap';

var StoryStrap = React.createClass({ 

  render: function() {

    return (
        <div>
            <Input type="text" placeholder="Headline" />
            <Input type="text" placeholder="Subline" />
        </div>
    );
  }

});

module.exports = StoryStrap;

How can I display this component from the return method of a second component in a different file?

MainSection.react.js

return (
    <div className="container">
        <div className="row row-grid">
            <section id="ROTopBar" className="ro-topBar box-shadow--2dp">

                <div id="storyStraps" className="col-md-4">
                    <h4>Story Straps</h4>
                    <StoryStrap /> //Display storyStrap component here
                </div>

                <div id="storyNames" className="col-md-4">
                    <h4>Name</h4>
                </div>

                <div id="storyInfo" className="col-md-4">
                    <h4>Info</h4>
                </div>

            </section>
        </div>
    </div>
);
1
  • So your question is about sharing variables across scripts? Have you ever written modular javascript? You are using a mixture (for some reason) of require and import so you already have the ability to import it..? Commented Dec 9, 2015 at 14:13

1 Answer 1

1

You need to require the component.

var StoryStrap = require('./StoryStrap.react.js');

Then you can use <StoryStrap /> in your render function.

It's probably best to try and be consistent with your module import/export syntax. Either use the new ES6 module syntax or the older commonjs style.

Default Export

// ES6 modules
export default React.createClass({ ... });

// ES5 & commonjs
module.exports = React.createClass({ ... });

Default Import

// ES6 modules
import StoryStrap from './StoryStrap.react.js';

// ES5 & commonjs
var StoryStrap = require('./StoryStrap.react.js');

Named Exports

// ES6 modules
export const StoryStrap = React.createClass({ ... });

// ES5 & commonjs
exports.StoryStrap = React.createClass({ ... });

Named Import

// ES6 modules
import { StoryStrap } from './StoryStrap.react.js';

// ES5 & commonjs
var StoryStrap = require('./StoryStrap.react.js').StoryStrap;
Sign up to request clarification or add additional context in comments.

8 Comments

doesn't need the extension
Nope, it doesn't but the asker is clearly new to modules and they've used a strange file extension, so I thought it would be best to be explicit.
Hi Have tried exactly what have written, but nothing is returned when I utilise <StoryStrap /> not even any console errors.
Try console.log(StoryStrap) from your main section file. See what comes out.
nothing seems to come out in the console at all, although I have just seen this error: Warning: the query argument to createHref is deprecated; use a location descriptor instead But is todo with the React Router
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.