简体   繁体   English

装入组件时不加载导入的脚本-React JS

[英]Imported Script doesn't load when a component is mounted - React JS

I am working on a React JS application. 我正在开发一个React JS应用程序。 I have three main Routes which are /login , /register and, /app . 我有三个主要Routes ,分别是/login/register/app In the app component I have imported a swipe.js script that's only necessary for /app component and not for the rest. 在应用程序组件中,我导入了swipe.js脚本,该脚本仅对/app组件是必需的,而对于其余部分则不需要。 But when I run the app, it automatically loads the script on /login and /register as well, this is the first issue. 但是,当我运行该应用程序时,它会自动在/login/register上加载脚本,这是第一个问题。 The second issue is, when I login or register, then I wrote a script to get redirected to /app component and it is done successfully, but the as the swipe.js is already loaded, the events and functions from swipe.js doesn't work on /app . 第二个问题是,当我登录或注册,然后我写了一个脚本来重定向到/app组件,并将其成功完成,但作为swipe.js已经加载, eventsfunctionsswipe.js没有按”在/app上工作。 I don't know what is the problem. 我不知道是什么问题。

Index.js Code Index.js代码

 import React from 'react'; import ReactDOM from 'react-dom'; //Styles import './Components/assets/css/bootstrap.css' //Scripts import 'bootstrap' //Components import App from './App'; import Register from './Components/register' import Login from './Components/login' //Routing import { Route, BrowserRouter as Router } from 'react-router-dom'; ReactDOM.render( <Router> <Route path={'/app'} component={App} /> <Route path={'/login'} component={Login} /> <Route path={'/register'} component={Register} /> </Router> , document.getElementById('root')); 

App.js - /app App.js-/ app

 import React, {Component} from 'react'; //Scripts import 'bootstrap' import 'popper.js' //Custom Scripts import './Components/assets/js/swipe.min' // Components import Navigation from './Components/navigation' import SideBar from './Components/sidebar' import Chat from './Components/chat' import Compose from './Components/compose-modal' export class App extends Component { constructor(props) { super(props) this.state = { } } componentDidMount(){ console.log("App Component Mounted..."); } render() { return ( <div className="layout"> <Navigation /> <SideBar /> <Chat /> <Compose /> </div> ) } } export default App; 

Login & Register Components 登录和注册组件

 import React, { Component } from 'react' import './assets/css/bootstrap.css' import './scss/auth.scss' import AuthImage from './assets/img/chat-bg.jpg' import Profile from './assets/img/avatars/avatar-male-1.jpg' import { Link, Redirect } from 'react-router-dom'; import {app, Cookies} from '../Shared/Globals' export class register extends Component { constructor(props) { super(props) this.state = { fname:'', lname:'', email:'', pass:'', pass2:'', formValid : false, regFormBtn : 'Join Now', formError : { error : false, data : null }, registered : false } this.handleRegisterForm = this.handleRegisterForm.bind(this) this.registerUser = this.registerUser.bind(this) } handleRegisterForm(event){ this.setState({ [event.target.name]:event.target.value }) if(this.state.fname !== '' && this.state.lname !== '' && this.state.email !== '' && this.state.pass !== '' && this.state.pass2 !== '' ){ this.setState({ formValid : true }) } else{ this.setState({ formValid : false }) } } registerUser(event){ event.preventDefault(); if(this.state.formValid){ this.setState({ regFormBtn : 'Joining ...', formValid : false }) app.auth().createUserWithEmailAndPassword(this.state.email, this.state.pass) .then( res => { console.log(res); new Cookies().createCookie("uid", res.user.uid, '10 minutes'); this.setState({ regFormBtn : 'Success !' }) this.resetForm(); this.setState({ registered : true }) }) .catch( error =>{ console.log(error); this.setState({ formValid : true, regFormBtn : 'Try Again' }) let err = {...this.state} err.formError.error = true this.setState({ err }) switch(error.code){ case 'auth/email-already-in-use': case 'auth/weak-password': case 'auth/network-request-failed': { let err = {...this.state} err.formError.data = error.message this.setState({ err }) break; } default: } }) } else{ console.log("Form is invalid"); } } render() { let e = ''; if(this.state.formError.error){ e = <div className='error'>{this.state.formError.data}</div> } else{ e = '' } if(this.state.registered){ return <Redirect to='/app' /> } return ( <> <div className='container auth-row'> <div className='row'> <div className='col-6'> <div className='auth-img'> <img src={AuthImage} alt='auth' /> </div> </div> <div className='col-6' > <div className='auth'> <div className='auth-head'> Join Our Chat Community Now !</div> <div className='auth-form'> <form className='row' method='POST' onSubmit={this.registerUser}> <div className='col-12'> {e} </div> <div className='rcaInput col-6'> <input type='text' placeholder='First Name' name='fname' onChange={this.handleRegisterForm} /> </div> <div className='rcaInput col-6'> <input type='text' placeholder='Last Name' name='lname' onChange={this.handleRegisterForm} /> </div> <div className='rcaInput col-12'> <input type='email' placeholder='Email Address' name='email' onChange={this.handleRegisterForm} /> </div> <div className='rcaInput col-12'> <input type='Password' placeholder='Password' name='pass' onChange={this.handleRegisterForm} /> </div> <div className='rcaInput col-12'> <input type='Password' placeholder='Repeat Password' name='pass2' onChange={this.handleRegisterForm} /> </div> <div className='btn-space col-12'> <button className='rcaBtn' type='submit' disabled={!this.state.formValid}> {this.state.regFormBtn} </button> <div className='small-font' style={{paddingTop:'10px'}}> Already have an account ? <Link to={'/login'} > Login Now</Link> </div> </div> </form> </div> </div> </div> </div> </div> </> ) } } export default register 

In app.js file you can modify it slightly: 在app.js文件中,您可以对其进行一些修改:

let swipe;

export class App extends Component {
  constructor(props) {
    super(props)

    this.state = { swipeLoaded: false };
  }

  componentDidMount() {
    swipe = require('./...swipe.js'); // require your swipe file here
    this.state = { swipeLoaded: true };
  }
}

To prevent other issues in your render (because setting state in CDM will cause a re-render) add this to the render(): 为了防止渲染中出现其他问题(因为CDM中的设置状态将导致重新渲染),请将其添加到render()中:

if (!this.state.swipeLoaded) return null;

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

相关问题 捆绑为 UMD 的 React 组件在通过另一个 React 应用程序中的脚本标签导入时无法加载依赖项 - React component bundled as an UMD can't load a dependency when imported via script tag in another react app 如何在react js中加载脚本? 脚本不起作用 - How to load script in react js? script doesn't work React Bootstrap 组件在我的 App.js 中加载时没有出现 - React Bootstrap component doesn't appear when I load it in my App.js React JS - 无法在尚未安装的组件上调用 setState - React JS - can't call setState on a component that is not yet mounted 为什么控制台登录库在导入到不同的 React 组件后不起作用? - Why console log on library doesn't work, when it has been imported to a different react component? React Hook组件不会在导入的阵列更改时更新 - React Hook component doesn't update on imported array change 导入的JS脚本在ASP.NET视图中不起作用 - Imported JS script doesn't worked in ASP.NET view Javascript 在安装组件时反应 function - Javascript in react function when component is mounted 即使组件正在渲染,React route也不会加载组件 - React route doesn't load component even if the component is rendering js文件在Angular项目中导入时不起作用 - Js file doesn't work when imported in Angular project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM