React-Bootstrap ES6 Imports

Datetime:2016-08-22 21:35:15          Topic: Bootstrap  React  ECMAScript           Share

When leveraging React Bootstrap , one important consideration missing from the documentation (or perhaps, simply not emphasized enough) relates to module access when using ES6 imports .

Specifically, in the context of React Bootstrap’s “convenience components” <Component.SubComponent> (e.g. <Modal.Body> ), such imports must be made explicit as they can not be resolved against their parent components during transformations of ES6 imports to CommonJS modules.

For instance, one can not reference sub-components as follows:

 
// import Navbar ...
import {Navbar} from 'react-bootstrap';
...
{/* reference Text component as a sub-component of Navbar */}
<Navbar.Text>...</Navbar.Text>
 

During transpilation, the above reference will result in Babel (specifically, the babel-plugin-transform-es2015-modules-commonjs module) causing an upstream exception:

Property object of JSXMemberExpression expected node to be of a type [“JSXMemberExpression”,”JSXIdentifier”] but instead got “MemberExpression

Fortunately, the solution to the issue is rather straight-forward; simply import the sub-component explicitly via react-bootstrap/lib , for example:

 
// import NavbarText ...
import {Text} from 'react-bootstrap/lib/Navbar';
...
{/* reference Text component */}
<Text>...</Text>
 

With the above example, the previous conversion errors will be resolved, and all will be well again.

Overall, I actually prefer the explicit imports (though it would be more convenient if they were exported via ‘react-bootstrap’).

And so, for the time being, a minor nuance worth noting should you find yourself trying to diagnose this issue at some point.





About List