[英]Problem importing stateless function is react
I am very new to react and having a bizarre problem. 我很反应并且有一个奇怪的问题。 I have defined a stateless function and when i want to try to import it in my main component it is not loading the function.
我已经定义了一个无状态函数,当我想尝试在我的主要组件中导入它时,它没有加载该函数。 i am guessing there is a naming convention that i dont know of.
我猜有一个我不知道的命名约定。 npm start does not give any error so I am assuming the code is compiling ok.
npm start没有给出任何错误,所以我假设代码正在编译好。
My stateless component is below 我的无国籍组件如下
import React from 'react';
const AppHeader = () => {
return(
<div class="jumbotron text-center">
<h1>Map Search</h1>
<p>Searching map for nearest matches from account</p>
</div>
);
}
export default AppHeader;
below does not work, 下面不起作用,
import React from 'react';
import './App.css';
import appHeader from './UI/appHeader';
class App extends React.Component {
render(){
return (
<div className="App">
<appHeader/>
</div>
);
}
}
export default App;
VS code lint says VS代码lint说
appHeader is declared but its value is never used.
appHeader已声明,但其值从未使用过。
However below does work, 但是下面的确有效,
import React from 'react';
import './App.css';
import KKK from './UI/appHeader';
class App extends React.Component {
render(){
return (
<div className="App">
<KKK/>
</div>
);
}
}
export default App;
VS code lint does not show the error anymore also the app is working as expected. VS代码lint不再显示错误,应用程序也按预期工作。 As you can see i only changed the name of the appHeader component to KKK.
如您所见,我只将appHeader组件的名称更改为KKK。 can someone point out what am i doing wrong and may be provide any documentation around this.
有人可以指出我做错了什么,并可能提供任何相关的文件。
You need to capitalize appHeader
to be AppHeader
in order for React to understand that this is a custom component and not a built in html component. 您需要将
appHeader
大写为AppHeader
,以便React了解这是一个自定义组件而不是内置的html组件。 Components that start with lowercase are assumed to be built in HTML elements. 假设以小写字母开头的组件构建在HTML元素中。
Check out this answer: ReactJS component names must begin with capital letters? 看看这个答案: ReactJS组件名称必须以大写字母开头?
And from the React docs: 并从React文档:
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.
当元素类型以小写字母开头时,它引用一个内置组件,如或,并导致传递给React.createElement的字符串'div'或'span'。 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.
以大写字母开头的类型,如编译为React.createElement(Foo),对应于在JavaScript文件中定义或导入的组件。
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.
如果您确实有一个以小写字母开头的组件,请在JSX中使用它之前将其分配给大写变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.