简体   繁体   English

React js 组件渲染

[英]React js components render

Hello I'm starting in react for now and I'm a little confused, I'm using routes / private routes, and so I created a components folder where I will have my login components and my login / index.js folder where I import this component, but I'm No doubt what better way to do this:你好我现在开始反应,我有点困惑,我正在使用路由/私有路由,所以我创建了一个组件文件夹,我将在其中包含我的登录组件和我的 login / index.js 文件夹,我导入这个组件,但我毫无疑问有什么更好的方法来做到这一点:

my app.js:我的 app.js:

import React, { Component } from 'react';
import './App.css';

import Routes from './routes';

function App() {
  return (
          <Routes/>
  );
}

export default App;

my routes js:我的路线js:

export default function Routes(){
    return(
        <BrowserRouter>
            <Switch>
                <Route path="/" exact component = {Login}/> //só chama rota se o caminho for exato; 
                <PrivateRoute path="/home" component = {DashBoard}/>              
            </Switch>
        </BrowserRouter>
    );
}

my component / LoginForm:我的组件/登录表单:

import React, { Component } from 'react';

class LoginForm extends Component {
    constructor(props){
        super(props);
        this.state = {
            login:'',
            password:'',
            errors: {},
            isLoading: false
        };
        this.onSubmit = this.onSubmit.bind(this);
        this.onChange = this.onSubmit.bind(this);
    }
    onSubmit(e){
        e.preventDefault();
    }
    onChange(e){
        this.state({[e.target.name]: e.target.value});
    }

  render() {
    const { errors, login, password, isLoading } = this.state;
    return (
        <form onSubmit={this.onSubmit}>
            <label for="login">Login</label>
            <input type="text" id="login" value={login} error= {errors.login} onChange={this.onChange} placeholder="Informe seu login" />
            <label for="password">Senha</label>
            <input type="password" id="password" value={password} error= {errors.password} onChange={this.onChange}   placeholder="Informe sua senha"/>
        <button className="btnEnt" type="submit" disabled ={isLoading}>Entrar</button>
    </form>
    )
  }
}

export default LoginForm;

Now i'm in doubt as i will call my login comp in my index.js file from my login page现在我有疑问,因为我将在我的登录页面中的 index.js 文件中调用我的登录 comp

index.js index.js

import React, { Component } from 'react';
import loginComp from '../../components/LoginForm';


class Login extends Component {
    render() {
      return (
          <loginComp />
      )
    }
  }

  export default Login;

I don't have an error, but it doesn't render anything on screen我没有错误,但它不会在屏幕上呈现任何内容

Looks like you have a typo right here: <ionChange} placeholder="Informe sua senha"/> and you repasted the bottom portion of the code.看起来您在这里有一个错字: <ionChange} placeholder="Informe sua senha"/>并且您重新粘贴了代码的底部。

Assuming this was a mistake and successfully compiles, you're only going to want that bottom export default and make sure your pathing is correct to that file.假设这是一个错误并成功编译,您只需要底部export default并确保您的路径对该文件是正确的。

./pages/login does not exist, I believe you have named your login file LoginForm ./pages/login不存在,相信你已经将你的登录文件命名为LoginForm

Also, I am pretty sure .另外,我很确定。 you need a div inside BrowserRouter, like so:您需要在 BrowserRouter 中创建一个 div,如下所示:

export default function Routes(){
    return(
        <BrowserRouter>
          <div>
            <Switch>
                <Route path="/" exact component={Login}/> 
                <PrivateRoute path="/home" component={DashBoard}/>              
            </Switch>
          </div>
        </BrowserRouter>
    );
}

Since, <loginComp/> is not a component you have created in LoginPage it is not rendering the data.由于<loginComp/>不是您在 LoginPage 中创建的组件,因此它不会呈现数据。 You can simply change the你可以简单地改变

   import loginComp from '../../components/LoginForm';

to  import LoginForm from '../../components/LoginForm'; 

remember /components/LoginForm this is the file name you have saved and not the component you have created.请记住 /components/LoginForm 这是您保存的文件名,而不是您创建的组件。

 render() {
      return (
          <LoginForm/>
      )
    }

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

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