Reputation: 797
I encounter a strange issue when I export
a const
to another js file. Here is my issue:
Imagine I have two files named index.js
and router.js
.
// in router.js
export const stackNav = StackNavigator({
Home: {
screen: Me,
navigationOptions: {
title: 'Welcome'
}
}
});
// in index.js
import { stackNav } from './router';
class MainApp extends Component {
render() {
return (
<stackNav />
);
}
}
export default MainApp;
When I use the above code to run, I fail to run my app and it shows error message with red screen: Expected a component class, got [object Object].
However, if I change all stackNav
to StackNav
, I can run my app successfully. So, I don't know why the case of the name/identifier matters?
Upvotes: 0
Views: 227
Reputation: 797
Referring to Offical doc,
User-Defined Components Must Be Capitalized
When an element type starts with a lowercase letter, it refers to a built-in component like or and results in a string 'div' or 'span' passed to React.createElement.
Types that start with a capital letter like compile to React.createElement(Foo) and correspond to a component defined or imported in your JavaScript file.
We recommend naming components with a capital letter. If you do have a component that starts with a lowercase letter, assign it to a capitalized variable before using it in JSX.
The following are the code snippet from the doc.
import React from 'react';
// Wrong! This is a component and should have been capitalized:
function hello(props) {
// Correct! This use of <div> is legitimate because div is a valid HTML tag:
return <div>Hello {props.toWhat}</div>;
}
function HelloWorld() {
// Wrong! React thinks <hello /> is an HTML tag because it's not capitalized:
return <hello toWhat="World" />;
}
Upvotes: 0
Reputation: 4386
Because React/ReactNative component name must begin with capital letters
Upvotes: 1