繁体   English   中英

将 react 中的组件渲染为变量 vs jsx 组件

[英]Rendering the component in the react as variable vs jsx component

以下代码取自 crate react app

        import React from 'react';
        import ReactDOM from 'react-dom';
        import './index.css';
        import App from './App';
        import * as serviceWorker from './serviceWorker';
        import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
        
        ReactDOM.render(<App/>, document.getElementById('root'));
        
        
        serviceWorker.register()

我写的下面的代码

const a = 
  (
    <div>
      <h1>Visibility Toogle </h1>
      <button>Show details</button>
  </div>
      );

    
ReactDOM.render(<a/>,document.getElementById('app'));

我在屏幕上看不到任何东西,只是一个空白页,但我更改了以下行现在很好

ReactDOM.render(a,document.getElementById('app'));

我已经看到了语法 我开始知道第一个元素必须是 jsx 认为这也是 jsx 但我想知道为什么我失败了

2)我尝试了 {a} 也我失败了也是 javascript 方法为什么它失败了

你有两个问题。

第一个:React 组件必须以大写开头,否则 React 认为它们是标准的 HTML 标签。

第二个:您没有渲染任何组件。 React 应用程序的根必须是一个组件。

因此,将您的代码更改为:

const Ex = () =>  
  (
    <div>
      <h1>Visibility Toogle </h1>
      <button>Show details</button>
  </div>
      );


ReactDOM.render(<Ex/>,document.getElementById('app'));

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM