【问题标题】:Rendering the component in the react as variable vs jsx component将 react 中的组件渲染为变量 vs jsx 组件
【发布时间】:2019-06-03 09:30:02
【问题描述】:

以下代码取自 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} 也失败了 a 也是 javascript 方法失败的原因

【问题讨论】:

标签: javascript reactjs


【解决方案1】:

你有两个问题。

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

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

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

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


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

【讨论】:

  • 当我将代码更改为 ReactDOM.render(a,document.getElementById('app'));但我想知道为什么
猜你喜欢
  • 2021-07-15
  • 2018-03-07
  • 2021-04-25
  • 2018-12-08
  • 2015-05-16
  • 1970-01-01
  • 2019-05-17
  • 2021-10-11
  • 2017-09-20
相关资源
最近更新 更多