首页 > 解决方案 > 没有输出 React Js for web

问题描述

我的反应应用程序没有输出,没有错误。首先它工作正常,现在同样的事情不起作用。

我尝试过 React.render 和 ReactDOM.render 我尝试过使用和不使用 render()

应用程序.js

import ReactDOM from 'react-dom';
import './index.css';

function Greeting()
{
  
    return
    (
      <h1>helllo</h1>
    );
  
}



ReactDOM.render
(
  <Greeting/>,document.getElementById('root')
);

--------------------------**Index.css**----------------------------------

    ```body {
      margin: 0;
      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
        'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
        sans-serif;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
    }
    
    code {
      font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
        monospace;
    } 

标签: javascriptcssreactjsoutput

解决方案


问题在于returnstatement( line-break issue),如果您在 中返回值next line,它将被视为undefined。这就是为什么你在 dom 上看不到任何东西的原因。阅读这些答案以获得更多理解

return
    (
      <h1>helllo</h1>
    );

改成

 return (
   <h1>helllo</h1>
 );

还要稍微格式化你的代码。

ReactDOM.render(
  <Greeting/>,document.getElementById('root')
);

推荐阅读