简体   繁体   English

我定义了组件并使用它 App.js 但出现未定义的错误

[英]I defined component and used it App.js but getting undefined error

When I try to access the relevant component under app.js, I get the error "WARNING in [eslint] rc\App.js Line 2:8: 'personAdd' is defined but never used no-unused-vars".当我尝试访问 app.js 下的相关组件时,我收到错误“[eslint] rc\App.js 第 2:8 行中的警告:'personAdd' 已定义但从未使用 no-unused-vars”。 When I run the project, I see tags in the form of in html, but the component does not appear on the screen.运行项目时,我看到 html 形式的标签,但该组件未出现在屏幕上。 I have included the codes below.我已经包含了下面的代码。 Thanks in advance.提前致谢。 Note: Changes under.eslintrc.json didn't work.注意:.eslintrc.json 下的更改不起作用。

App.js应用程序.js

 import React from 'react'; import personAdd from './screens/personAdd'; function App() { return ( <personAdd /> ) } export default App;

personAdd.js personAdd.js

 import React from 'react'; class personAdd extends React.Component{ render(){ return( <div id = "personAdd"> <h1>Kullanıcı Bilgileri</h1> <form> <label htmlFor="id">Ad</label> <input id="id"/> <button>Add</button> </form> </div> ) } } export default personAdd;

index.js index.js

 import React from 'react'; import ReactDOM from 'react-dom/client'; import './index.css'; import App from './App'; //import personAdd from './screens/personadd'; import reportWebVitals from './reportWebVitals'; const root = ReactDOM.createRoot(document.getElementById('root')); root.render( <React.StrictMode> <App /> </React.StrictMode> ); /*const personadd = ReactDOM.createRoot(document.getElementById('personadd')); personadd.render( <React.StrictMode> <personAdd /> </React.StrictMode> );*/

You don't need to manually create new root elements for every component you want to render.您不需要为要渲染的每个组件手动创建新的根元素。

React inserts an initial element 'root' into the DOM so that the app can render within that. React 会在 DOM 中插入一个初始元素“根”,以便应用程序可以在其中呈现。

Try removing:尝试删除:

/*const personadd = ReactDOM.createRoot(document.getElementById('personadd'));
personadd.render(
  <React.StrictMode>
    <personAdd />
  </React.StrictMode>
);*/

If you want to render your personAdd component you can add it as a child of App as you've already done.如果您想渲染您的 personAdd 组件,您可以将它添加为 App 的子组件,就像您已经完成的那样。

function App() {
    return (
      <personAdd /> 
    )
}

The other reason you're getting these issues is because you're not using Pascal case when naming your components (PersonAdd).您遇到这些问题的另一个原因是您在命名组件 (PersonAdd) 时没有使用 Pascal 大小写。

function App() {
    return (
      <PersonAdd /> 
    )
}

In addition as others have mentioned, stick to function components rather than class components.此外,正如其他人所提到的,坚持使用 function 组件而不是 class 组件。

I'd recommend having a look at the React Beta Docs which now do everything with functional components.我建议您查看React Beta Docs ,它现在可以使用功能组件完成所有操作。 There are helpful walkthroughs on there that should help you out.那里有一些有用的演练可以帮助你。

As @evolutionxbox said.正如@evolutionxbox 所说。 Try naming the component with UpperCamelCase.尝试使用 UpperCamelCase 命名组件。 It's used to specify a React element https://reactjs.org/docs/jsx-in-depth.html#specifying-the-react-element-type它用于指定一个 React 元素https://reactjs.org/docs/jsx-in-depth.html#specifying-the-react-element-type

Also, it is now common to create components as functions and not Classes.此外,现在通常将组件创建为函数而不是类。 Of course it's up to you if you do prefer classes.当然,如果你喜欢上课,这取决于你。 https://reactjs.org/docs/components-and-props.html#rendering-a-component https://reactjs.org/docs/components-and-props.html#rendering-a-component

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 定义的组件在 App.js 中不可访问 - Defined Component is not accessible in App.js [eslint] src\App.js 第 2:8 行中的 React js 错误警告:'person' 已定义但从未使用过 - React js error WARNING in [eslint] src\App.js Line 2:8: 'person' is defined but never used 未捕获的类型错误:无法在 app.js:22 处读取未定义的属性“RecaptchaVerifier”,这是我在浏览器中遇到的错误,请解决 - Uncaught TypeError: Cannot read property 'RecaptchaVerifier' of undefined at app.js:22,this is the error i am getting.in browser,please resolve it ./src/App.js Line 6:19: &#39;Component&#39; 未定义 no-undef - ./src/App.js Line 6:19: 'Component' is not defined no-undef 从类组件访问 App.js 中定义的变量 - Access variables defined in App.js, from class component 我创建了 navbar.js 文件,我在 app.js 中定义了它,但是我得到了这个错误。 我如何解决这个错误? - I created navbar.js file, I defined it in app.js ,But i got this error. How solve i this error? 在./src/App.js&#39;store&#39;中的反应错误未定义 - react Error in ./src/App.js 'store' is not defined 引用的错误:app.js第1行中未定义angular - Referenced Error: angular is not defined in app.js line 1 如何在React“ ./src/components/App.js&#39;Layout&#39;,&#39;Home&#39;中定义组件错误,未定义react / jsx-no-undef - How to fix component error in React "./src/components/App.js 'Layout', 'Home' is not defined react/jsx-no-undef React-Native:将组件导入App.js时出错 - React-Native : error while import component to App.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM