Home  >  Article  >  Web Front-end  >  How to resolve style conflicts in react

How to resolve style conflicts in react

藏色散人
藏色散人Original
2020-12-09 09:21:033623browse

Method to resolve style conflicts in react: First open the corresponding code file; then add the module name before the class name, for example, add the component name LoveVideo before the CSS class name of the entire component's style sheet.

How to resolve style conflicts in react

The operating environment of this tutorial: windows7 system, react17.0.1 version, thinkpad t480 computer.

Recommended: "Javascript Basics Tutorial"

Resolving style conflicts in react

There are many needs in react development Things to pay attention to, in other words, there are many pitfalls that need to be stepped on. Here I will share a pitfall that I encountered, which is style conflict. This is a noteworthy problem. First, take a look at the example:

There are two components, one is called TestAComponent and the other is called TestBComponent. In the TestA component I wrote a button with a blue background color, and in TestB I wrote a button with a red background color.

TestAComponent Component A:

class TestAComponent extends React.Component {
  render() {
    return (
      <div>
        <button className="box">背景为蓝色</button>
      </div>
    );
  }
}

TestA css, the background is set to blue:

.box{
  font-size: 20px;
  margin: 10px;
  padding: 20px;
  background-color: blue;
  border-radius: 10px;
}

TestB Component B:

class TestBComponent extends React.Component {
  render() {
    return (
      <div>
        <button className="box">背景为红色</button>
      </div>
    );
  }
}

TestB css, the background is set is red:

.box{
  font-size: 20px;
  margin: 10px;
  padding: 20px;
  background-color: red;
  border-radius: 10px;
}

After writing the code, npm start, you will find that the effect displayed in the browser is like this:

How to resolve style conflicts in react

Obviously two Different background colors are set for the buttons. Why is the actual display like this? This is where we analyze the style settings. The class name we set in the label is box. This is a common naming method used by many new front-end developers. However, setting the class names of labels of different components to the same name will cause style conflicts.

Method to solve this problem:

Add the module name before the class name. For example, if this component is called LoveVideo, add the component name LoveVideo before the CSS class name of the entire component's style sheet:

<div>
    <button className="LoveVideobox">TestA 背景为蓝色</button>  
</div>
.LoveVideobox{
  font-size: 20px;
  margin: 10px;
  padding: 20px;
  background-color: blue;
  border-radius: 10px;
}

Refresh the page after modification, and you will find that the problem of style conflict will be solved very well:

How to resolve style conflicts in react

In addition to the conflicts caused by the same naming, there are also There is a situation where some global styles are set, such as:

html{
  background-color: #fff;
  font-size: 14px;
}
*{
  margin: 0;
  padding: 0;
  background-color: #fff;
  font-size: 14px;
}

Such global styles must not be set, because using react to make a single-page application has only one page. If global styles are set, The entire page will be loaded with this style.

Summary

1. Add the component name prefix to a single component class name. If the component is named LoveVideo, add this prefix to all style names

2. Do not set it Global common styles such as html{}, *{}

The above is the detailed content of How to resolve style conflicts in react. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn