简体   繁体   English

反应:styles 被意外导入错误的模块

[英]React: styles being imported to wrong module unexpectedly

Im new to React JS, working on my first soon-to-be live site.我是 React JS 的新手,正在开发我的第一个即将上线的网站。 I have styled input in a form in a seperate component, then imported the styles to that component.我在单独的组件中以表单的形式设置了输入样式,然后将 styles 导入到该组件。 After implementing routing, it seems that this style is applied to forms across the site, whether or not i import them.实现路由后,无论我是否导入它们,似乎这种样式都应用于跨站点的 forms。 Note, i was initially putting all styles in index.css until I expanded the scope of the site, and this was no longer practical.请注意,我最初将所有 styles 放在 index.css 中,直到我扩展了站点的 scope,这不再实用。 It now seems that the index.css styles remain, even after i removed the styles im trying to get rid of from index.css.现在看来 index.css styles 仍然存在,即使我删除了 styles 我试图摆脱 index.ZC7A67A628FCBA222 Either that, or there is something wrong with my routing, importing, exporting, that im not seeing, which is causing this bug:要么,要么我的路由,导入,导出有问题,我没有看到,这导致了这个错误:

在此处输入图像描述

This style is being applied from a different component with its own imported stylesheet.此样式是从具有自己导入样式表的不同组件中应用的。 I want the sign up form to appear like a normal form input, but its hidden because of the styles applied to BrowsePhotos.js.我希望注册表单看起来像一个普通的表单输入,但由于 styles 应用于 BrowsePhotos.js,它被隐藏了。 I am completely unsure why this happens.我完全不确定为什么会发生这种情况。

here is my project directory tree这是我的项目目录树在此处输入图像描述

and here is some source code related to possible problem sources这是一些与可能的问题来源相关的源代码

src/components/auth/SignUp.js src/components/auth/SignUp.js

import React, { useState } from 'react'
import './auth.css';


const SignUp = () => {



    return(
        <form onSubmit={handleSubmit} className="App-sign-up">
            <label className="auth-field"> Email:
                <input onChange={handleEmailChange} type="text"/>

                { !emailError && <div className="error">{emailError}</div>}
            </label>

            <label className="auth-field"> Password:
                <input onChange={handlePasswordChange} type="password"/>

                { !passwordError && <div className="error">{passwordError}</div>}
                {/* should also define errors for the other offenders- length, expected characters */}
            </label>

            { password && 
                <label className="auth-field"> Password:
                    <input onChange={handleConfirmPasswordChange} type="password"/>
                    { !confirmPasswordError && <div className="error">{confirmPasswordError}</div>}
                    { password !== confirmPassword && <div className="error">Password and Confirm Password must match</div>  }
                </label>
            }
            <button type="submit">Create Account</button>
            <div>Already have an account? Sign In!</div>
        </form>
    )

}
export default SignUp;

src/components/photos/UploadForm.js src/components/photos/UploadForm.js

import React, { useState } from 'react';
import ProgressBar from './ProgressBar';
import './UploadForm.css'



// TODO: add a new controlled component for providing text to use
// as photo description/ alt text.
const UploadForm = () => {
    
    //equivalent to setting state to '' in class based component
    const [file, setFile] = useState(null);
    // const [description, setDescription] = useState(null);
    const [error, setError] = useState(null);


    const handleChange = event => {
    
        // reference to the selected file,
        // and a list of allowable image types
        const selected = event.target.files[0]
        const types = ['image/png','image/jpeg'] 


        if (selected && types.includes(selected.type)) {
            setFile(selected);
            setError('');
            // if (event.target.type === 'textarea') {
            //     console.log("we got a description")
            // }
        } else {
            setFile(null);
            setError('Enter a valid photo type : jpg or png')
        }


    }

    
        return (
            <form>
                <label>
                    <span>+</span>
                    <input onChange={handleChange} type="file"/>

                    {/* <div className="description">
                        <label className="inner-label">Description of the uploaded file</label>
                        <textarea cols="30" rows="10"
                        onChange={handleChange}
                        ></textarea>
                    </div> */}
                    
                </label>
                
                <div className="output">
                    { error && <div className="error" >{ error }</div>}
                    { file  && <ProgressBar file={file} setFile={setFile}/>}
                </div>
            </form>
          );
} 
export default UploadForm;


src/components/photos/UploadForm.css src/components/photos/UploadForm.css

form{
    margin: 30px auto 10px;
    text-align: center;
  }
  label input{
    height: 0;
    width: 0;
    opacity: 0;
  }
  label{
    display: block;
    width: 30px;
    height: 30px;
    border: 1px solid var(--primary);
    border-radius: 50%;
    margin: 10px auto;
    line-height: 30px;
    color: var(--primary);
    font-weight: bold;
    font-size: 24px;
  }
  label:hover{
    background: var(--primary);
    color: white;
  }
  .output{
    height: 60px;
    font-size: 0.8rem;
  }
  .error{
    color: var(--error);
  }
  



src/index.css src/index.css

@import url('https://fonts.googleapis.com/css2?family=Noto+Serif:wght@400;700&display=swap');

:root{
  --primary: #efb6b2;
  --secondary: #4e4e4e;
  --error: #ff4a4a;
}

/* base styles & title */
body{
  font-family: "Noto Serif";
  color: var(--secondary);
}
.App{
  max-width: 960px;
  margin: 0 auto;
}
.title h1{
  color: var(--primary);
  font-size: 1.2rem;
  letter-spacing: 2px;
  font-weight: normal;
}

src/components/general/title // note: home of routing, ive tried commenting out BrowsePhotos from routing. src/components/general/title // 注意:路由的主页,我尝试从路由中注释掉 BrowsePhotos。 this is then imported to app.js然后将其导入 app.js

import React from 'react';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
import './general.css'

import About from '../general/About';
import SignIn from '../auth/SignIn';
import SignUp from '../auth/SignUp';
import BrowsePhotos from '../photo/BrowsePhotos';
import BrowseProfiles from '../auth/BrowseProfiles';

const Title = () => {
  return (
    <div className="title">
      <Router>
            <nav style={{padding: "5px"}}>
                <h1 className="title">Oral-History</h1>

                <h1> 
                    <Link to="/about">About</Link>
                </h1>

                <h1> 
                    <Link to="/sign-in">Sign In</Link>
                    {/* becomes sign-out when user is signed in */}
                </h1> 
                
                <h1>
                    <Link to="/sign-up">Sign Up</Link>
                    {/* sign-up becomes 'my profile' after sign in */}

                </h1>
                
                <h1> 
                    <Link to="/profiles">Profiles</Link>
                </h1>
                
                <h1> 
                    <Link to="/photos">All Photos</Link>
                </h1>
            </nav> 
            
            <Route exact path="/about" component={About}/>
            <Route exact path="/sign-in" component={SignIn}/>
            <Route exact path="/sign-up" component={SignUp}/>
            <Route exact path="/profiles" component={BrowseProfiles}/>
            <Route exact path="/photos" component={BrowsePhotos}/>

        </Router>

      
    </div>
  )
}

export default Title;

I've also tried redefining the rule set for label and label input within SignUp.css, to set all the properties to default, or inherits: false, with no luck there.我还尝试在 SignUp.css 中重新定义 label 和 label 输入的规则集,将所有运气属性设置为默认值,或者继承:false,不

It sounds like the CSS is not being imported scoped but globally.听起来 CSS 不是在范围内而是在全局范围内导入的。 You could use a CSS-in-JS library like CSS-Modules or react-scoped-css to fix this.您可以使用 CSS-in-JS 库(如CSS-Modulesreact-scoped-css )来解决此问题。

Another way would be to give the html-elements classes and not style them directly.另一种方法是提供 html-elements 类而不是直接设置它们的样式。 That is usually more scalable.这通常更具可扩展性。

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

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