Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  82] [ 6]  / answers: 1 / hits: 28160  / 8 Years ago, fri, april 15, 2016, 12:00:00

I have two modules that I want to share a const array. One of these modules includes both the const array and a component, whilst the other module only includes a component.



This is what I have in module A.



export const ORDER_COLUMNS = [
{ name: 'orderNumber', title: 'Order', width: '10%', type: 'string' },
{ name: 'orderType', title: 'Type', width: '10%', type: 'string' }
];

class OrderGridControl extends React.Component {
constructor(props) {
super(props);
this.state = {
orderColumns: ORDER_COLUMNS
};
}
...

}

export default OrderGridControl;


And in module B.



import {OrderGridControl, ORDER_COLUMNS} from 'component/order-grid-control';

class OrderQueryPage extends React.Component {
constructor(props) {
super(props);
this.state = {
orderColumns: ORDER_COLUMNS
};
console.info(this.state.orderColumns);
}

...

render() {
return (
<div>
<PropertyGrid gridSetup={this.state.orderColumns} />
</div>
);
}
}


When I run this I get the following error. invariant.js:39 Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. Check the render method of 'moduleB'.



However, the console.info(this.state.orderColumns) line logs all the column objects I expect.



Interestingly, if I copy the array into module B and assign the columns in the constructor exactly the same way it seems to work. It only seems to be an issue when I'm importing from the other module.


More From » reactjs

 Answers
14

You've got it almost right-- you're exporting a default export (OrderGridControl) and a named export (ORDER_COLUMNS).



However, in B.js, you're trying to import two named exports.



Modify your import to look like this:



import OrderGridControl, { ORDER_COLUMNS } from 'component/order-grid-control';


The advantage of having a default export is that you don't have to match its name exactly when importing it, so you could do something like



import GridControl, { ORDER_COLUMNS } from 'component/order-grid-control';

[#62547] Wednesday, April 13, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stephonkeandrer

Total Points: 392
Total Questions: 94
Total Answers: 100

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
;