【问题标题】:How do I make a new CSS file for a new component in React JS?如何为 React JS 中的新组件创建新的 CSS 文件?
【发布时间】:2021-12-15 21:54:56
【问题描述】:

现在我在一个名为Blog.js 的新窗口中创建了另一个组件。内嵌样式很方便,但最终会变得拥挤且难以编辑。我想制作另一个名为blog.css 的样式表,但是当我在其中设置样式时,它会改为样式App.js 组件。编辑:只是添加我的其余代码,我不想添加blog.module.css,因为它只有几个样式(h2 带有颜色和样式更改)。

App.js:

import React from "react"
import { Component } from "react"
import fbook from "./image/fbook.png"
import insta from "./image/insta.png"
import tweet from "./image/tweet.png"
import me from "./image/itsme.png"
import Blog from "./components/Blog.js"
import NewWindowComponent from "./NewWindowComponent"

class App extends Component {
  constructor(props) {
    super(props)
    this.state = {
      isNewWindow: false
    }

    this.handleChange = this.handleChange.bind(this)
    this.handleClick = this.handleClick.bind(this)
    
    
  }

  handleChange (event) {
    const {name, value} = event.target
      this.setState({name: value})
  }

  handleClick () {
    this.setState({
      isNewWindow: true
    })

  }



    render() {
      return (
    
      
        <body>
          <header>
            <h2>ALEX</h2>
              <div className="nav">
                  <a href="#">ALL</a>
                  <a href="#">TRAVEL</a>
                  <a href="#">LIFESTYLE</a>
                  <a href="#">MUSIC</a>
                  <a href="#">VIDEO</a>
              </div>

              <div className="search-box">
                <input type="text"
                className="search-bar"
                name="stuff"
                onChange={this.handleChange}
                value={this.state.value}
                placeholder="Search" />

              </div>
          </header>

              <div id="background">
              <div className= "container">
                <h1>HI! I AM ALEX!</h1><hr/>
                <p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
                
                <NewWindowComponent
                  onClose={this.state.isNewWindow}>
                  <Blog /> //want to style this component
                </NewWindowComponent>

                <button onClick= {this.handleClick}>GO TO BLOG</button> 
                <img className= "my-pic" src={me} alt="pic of me"/> 
              </div>
              </div>

    

              <footer>
                <div className="icons">
                  <img className ="f-book" src={fbook} alt="Facebook"/>
                  <img className ="tweet" src={tweet} alt="Twitter"/>            
                  <img className ="insta" src={insta} alt="Instagram"/>
                </div>

              
                <p>© 2021, Designed by Alex </p>
              </footer>
            
        </body>
        
      
      )
    }
}

export default App

blog.css 文件中,我只是想为Blog.js 中的h2 标签设置样式,使其变为红色和斜体,但就像我之前所说的那样,它在App.js 中设置了h2 标签的样式,即使我已经在@ 中设置了样式987654329@.

Blog.js:

import React from "react"
import { Component } from "react";
import BlogCSS from '../components/Blog.module.css'; //clashes with App.js for some reason

class Blog extends Component {



render() {
    const myStyle = {
        fontStyle: 'italic',
        fontWeight: 'lighter',
        color: 'red',
        paddingLeft: 450,
        position: 'relative',
        fontSize: 50
    }

    const scrim = {
        backgroundColor: 'black'
    }

    const woah = {
        lineHeight: 1.5,
        fontSize: 20
    }

    return (

        
        <body>
            <header>
                <div style={scrim}className="background">
                <h2 style={myStyle}>Welcome to my Blog!</h2> //want to style this tag
                </div>
            </header>

            <div id="background">
            <div className= "container">
            <h2>About me I guess...</h2><hr/>
            <p style={woah}>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.           </p>
            </div>
            </div>
        </body>
    )
 }
}
export default Blog

NewWindowComponent.js:

import { Component } from "react"
import ReactDOM from 'react-dom';


class NewWindowComponent extends Component {

 containerEl = document.createElement('div');


 externalWindow = null;


componentDidMount() {
  
  this.externalWindow = window.open('', 'NewWindowComponent');

  
  if (this.externalWindow)
  {
    this.externalWindow.document.body.appendChild(this.containerEl);
    this.externalWindow.onunload = () => this.props.onClose();
  }
}

render() {
  return ReactDOM.createPortal(this.props.children, this.containerEl);
}

}

export default NewWindowComponent

index.js:

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


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


reportWebVitals();

【问题讨论】:

    标签: css reactjs styles


    【解决方案1】:

    您可以使用css modules

    1. filename.module.css重命名你的css文件
    2. 在要使用的文件中导入import styles from './filename.module.css';
    3. 在元素&lt;h2 class={style.yourClassName}&gt;中使用它

    如果这对您没有帮助,请与 css 文件共享完整代码

    【讨论】:

    • 使用第一步将 css 文件更改为模块后,它不起作用。当我将index.css 配置为index.module.css 并在我的App.js 中进行测试时,它起作用了。出于某种原因,当我对blog.module.css 执行相同操作并导入Blog.js 时,它会使h2 标签消失。我的其余代码在上面有问题中进行了编辑。非常感谢您的帮助。
    • 另外,当我尝试导入 blog.module.css 时,它会立即与 App.js 发生冲突
    猜你喜欢
    • 2012-07-10
    • 2022-11-24
    • 2018-10-01
    • 2015-10-11
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    相关资源
    最近更新 更多