I am from Angular Background
and as far as I know. Each component has its own beauty and till we import a CSS file inside it
those CSS classes should not be applied even if we add to HTML Elements to the component in case we have not added or imported any CSS files for classes used inside this new/2nd component
.
What I did in React
was made 2 components - Home1.js
and Home2.js
. When I am importing a css file named Home.css to Home1 Component
and NOT to Home2.js component
- How in the world is those CSS classes affect being applied even too Home2 Component
. This is sheer absurd.
That's why I am importing someFile.css **specifically** to the component where I want its affect to be there
. This provided a kind of encapsulation affect
, which I am seeing is failing. Now, someone can explain me how in the world, wherever I am not importing someFile.css, there also the classes are having affect?
Home1.js
import React, { Component } from 'react';
import './home.css';
class Home1 extends Component {
render() {
return (
<div>
<div className="red">1...</div>
<div className="green">2...</div>
<button>click</button>
</div>
)
}
}
export default Home1;
Home2.js
import React, {useState} from 'react';
const Home2 = () => {
return (
<div>
<div className="red">1..</div>
<div className="green">2...</div>
<button>click</button>
</div>
)
}
export default Home2;
Result: