0

Right now I made another component that is on a new window called Blog.js. In-line styling is convenient but eventually it will get crowded and annoying to edit. I want to make another stylesheet called blog.css but when I style in it, it styles App.js component instead. EDIT: just adding the rest of my code, I didn't want to add the blog.module.css because it only has a few styles (h2 with color and styling change).

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

In the blog.css file I just wanted to style the h2 tag from Blog.js to make it red and italic but like I said before it styles the h2 tag in App.js even though I already have it styled in index.css.

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();

1 Answer 1

3

You can use css modules

  1. Rename your css files with filename.module.css
  2. Import it in the file you want to use it import styles from './filename.module.css';
  3. Use this in the element <h2 class={style.yourClassName}>

Share the complete code with css files if this doesn't help you

Sign up to request clarification or add additional context in comments.

2 Comments

It isn't working after using your first step to change the css files to modules. It worked when I configured the index.css to index.module.css and tested in my App.js. For some reason when I do the same for blog.module.css and import in Blog.js it makes the h2 tag just vanish. Rest of my code is edited in above in question. Thanks a lot for your help.
Also when I try to import the blog.module.css it immediately clashes with App.js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.