1

I am trying to setup small reactjs application and I am using Router.

  1. Login component - separate Css file
  2. Logout component - separate css file

While landing into the login page css is reflecting but while navigating to logout page instead of logout css login component page css is reflecting. How to fix this issue.

Note: importing styles in each component

Do we don't have any options like while loading component will load this css file like configuration?

Login.jsx

import React, { Component } from 'react'
import PropTypes from 'prop-types'
import "../Login/LoginStyle.css";

class Login extends Component {
    static propTypes = {

    }

    render() {
        return (
            <div>
                 <form className="box">
                    <h1>Login</h1>
                    <input type="text" name="" placeholder="Username" />
                    <input type="password" name="" placeholder="Password" />
                    <input type="button" name="" value="Login" />
                </form>
            </div>
        )
    }
}

export default Login

LoginStyle.css

body {
    margin: 0;
    padding: 0;
    font-family: sans-serif;
    background: red;
}

Logout.jsx

import React, { Component } from 'react'
import PropTypes from 'prop-types'
import "../Logout/LogoutStyle.css";

class Logout extends Component {
    static propTypes = {

    }

    render() {
        return (
            <div>
                Logged out successfully
            </div>
        )
    }
}

export default Logout

LogoutStyle.css

body{
    background: grey;
}

navigating to the login page background color should be red and in logout component load background color should be grey. How to fix this?

2 Answers 2

1

Since your application has two css files, both will be present whether you are on Login Page or Logout. To resolve your case you have to add classes to your component and add specific css according to that to distinguish.

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

1 Comment

I am importing CSS files in separate components (Login and Logout) even its taking styles for both components. How to fix this
0

In react application there is only one HTML file, which the index.html in public folder, and also all css file react-js bind to one file at the end, so it ll reflect first css property's of body tag, i hope it make sense

Comments

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.