简体   繁体   English

如何正确渲染 React.js - 不渲染

[英]How to render React.js properly - not rendering

I'm trying to create a web platform where I have two pages, one is index.html, created by system and other files associated with it.我正在尝试创建一个 Web 平台,其中有两个页面,一个是 index.html,由系统和与其关联的其他文件创建。 There is another loginSignup.html created by me and files related to it.我创建了另一个 loginSignup.html 和与之相关的文件。 I used the same approach as index.html but it is rendering nothing.我使用了与 index.html 相同的方法,但它没有渲染任何内容。

Here is me loginSignup.js code :这是我的 loginSignup.js 代码:

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


ReactDOM.render(<LoginSignup />, document.getElementById('root1'));


Here is test.js :这是 test.js :

import React from 'react';
import './App.css';
import './font-awesome.css'



function LoginSignup() {

    return <div>

        <form className="form-signin">
            <div className="text-center mb-4">
                <img className="mb-4" src="icon.svg" alt="" width="72" height="72"/>
                <h1 className="h3 mb-3 font-weight-normal">Assignment Submission System</h1>
                <p>Please Sign-in or Sign-up to continue</p>
            </div>

            <div className="form-label-group">
                <input type="email" id="inputEmail" className="form-control" placeholder="Email address" required
                       autoFocus/>
                <label htmlFor="inputEmail">Email address</label>
            </div>

            <div className="form-label-group">
                <input type="password" id="inputPassword" className="form-control" placeholder="Password" required>
                    <label htmlFor="inputPassword">Password</label>
                </input>
            </div>

            <div className="checkbox mb-3">
                <label>
                    <input type="checkbox" value="remember-me"> Remember me</input>
                </label>
            </div>
            <button className="btn btn-lg btn-primary btn-block btn-animated" type="submit">Sign in</button>
        </form>
    </div>

}


export default LoginSignup;


Inside loginSignup.html :在 loginSignup.html 中:


<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root1"></div>


index.js is here : index.js 在这里:


import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import './font-awesome.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { BrowserRouter } from 'react-router-dom';

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

setTimeout(function(){
    window.location.href = '/loginSignup.html';
}, 1000);




serviceWorker.unregister();


It renders noting..它让人注意到..

It is better to use either components or pure functions if you do not need lifecycle hooks. 如果不需要生命周期挂钩,最好使用组件或纯函数。

class LoginSignUp extends React.Component {

   ...
   render() {
     return <div>....</div>
   }
}

or Pure functions 或纯函数

export const LoginSignUp = () => {}

Try in return use () 尝试退货使用()

return (
<div>.....</div>
);

在此处输入图片说明

I am able to compile and see in the output of your form 我可以编译并在您的表单输出中看到

Your HTML layout for <input> is incorrect, refer the below correct JSX 您的<input> HTML布局不正确,请参考以下正确的JSX

import React from "react";
import ReactDOM from "react-dom";
//import "./index.css";
//import LoginSignup from "./test";
export default function LoginSignup () { /*move this portion in separate file*/

  return (<div>

      <form className="form-signin">
          <div className="text-center mb-4">
              <img className="mb-4" src="icon.svg" alt="" width="72" height="72"/>
              <h1 className="h3 mb-3 font-weight-normal">Assignment Submission System</h1>
              <p>Please Sign-in or Sign-up to continue</p>
          </div>

          <div className="form-label-group">
              <input type="email" id="inputEmail" className="form-control" placeholder="Email address" required
                     autoFocus/>
              <label htmlFor="inputEmail">Email address</label>
          </div>

          <div className="form-label-group">
              <input type="password" id="inputPassword" className="form-control" placeholder="Password" required />
                  <label htmlFor="inputPassword">Password</label> 
          </div>

          <div className="checkbox mb-3">
              <label>
                  <input type="checkbox" value="remember-me"/> Remember me 
              </label>
          </div>
          <button className="btn btn-lg btn-primary btn-block btn-animated" type="submit">Sign in</button>
      </form>
  </div>)

}
ReactDOM.render(<LoginSignup />, document.getElementById("root1"));

export your function like the below 像下面那样导出你的函数

export default function LoginSignup() {
    return <div>

        <form className="form-signin">
            <div className="text-center mb-4">
                <img className="mb-4" src="icon.svg" alt="" width="72" height="72"/>
                <h1 className="h3 mb-3 font-weight-normal">Assignment Submission System</h1>
                <p>Please Sign-in or Sign-up to continue</p>
            </div>

            <div className="form-label-group">
                <input type="email" id="inputEmail" className="form-control" placeholder="Email address" required
                       autoFocus/>
                <label htmlFor="inputEmail">Email address</label>
            </div>

            <div className="form-label-group">
                **<input type="password" id="inputPassword" className="form-control" placeholder="Password" required/>
                    <label htmlFor="inputPassword">Password</label> 
            </div>**

            <div className="checkbox mb-3">
                <label>
                    **<input type="checkbox" value="remember-me"/> Remember me** 
                </label>
            </div>
            <button className="btn btn-lg btn-primary btn-block btn-animated" type="submit">Sign in</button>
        </form>
    </div>

}

Highlighted** issues with your JSX HTML code JSX HTML代码的突出**问题

As Kannan G already mentioned, you have errors in your JSX. 正如Kannan G已经提到的那样,您的JSX中有错误。

I've been able to fix it so it renders at least: https://codesandbox.io/embed/silly-sea-07ej2 我已经能够修复它,因此它至少可以呈现: https : //codesandbox.io/embed/silly-sea-07ej2

Here is only the Test.js file if you don't want to click the link above: 如果您不想单击上面的链接,那么这里只是Test.js文件:

import React from "react";
import './App.css';
import './font-awesome.css';

function LoginSignup() {
  return (
    <div>
      <form className="form-signin">
        <div className="text-center mb-4">
          <img className="mb-4" src="icon.svg" alt="" width="72" height="72" />
          <h1 className="h3 mb-3 font-weight-normal">
            Assignment Submission System
          </h1>
          <p>Please Sign-in or Sign-up to continue</p>
        </div>

        <div className="form-label-group">
          <input
            type="email"
            id="inputEmail"
            className="form-control"
            placeholder="Email address"
            required
            autoFocus
          />
          <label htmlFor="inputEmail">Email address</label>
        </div>

        <div className="form-label-group">
          <input
            type="password"
            id="inputPassword"
            className="form-control"
            placeholder="Password"
            required
          />
          <label htmlFor="inputPassword">Password</label>
        </div>

        <div className="checkbox mb-3">
          <label>
            <input type="checkbox" value="remember-me" /> Remember me
          </label>
        </div>
        <button
          className="btn btn-lg btn-primary btn-block btn-animated"
          type="submit"
        >
          Sign in
        </button>
      </form>
    </div>
  );
}

export default LoginSignup;

Also I recommend you installing the react developer tools. 我也建议您安装react开发人员工具。

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

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