Home > Web Front-end > JS Tutorial > How to write style in react

How to write style in react

coldplay.xixi
Release: 2020-12-22 16:09:53
Original
12200 people have browsed it

How to write style in react: 1. Use inline style; 2. Use className method; 3. Use classnames to dynamically modify the style; 4. Use the [styled-components] plug-in to write label styles.

How to write style in react

The operating environment of this tutorial: windows7 system, React17 version. This method is suitable for all brands of computers.

How to write style in react:

1. Inline

import React, { Fragment } from "react";
class Style extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
      const txtColor = {
          color: '#F00'
      }
    return (
     <Fragment>
         <h1 style={txtColor}></h1>
     </Fragment>
    );
  }
}
export default Style;
Copy after login

This writing method is not recommended, style If there are too many, the code will be messy!

2. Use className

import React, { Fragment } from "react";
import "./../../style.css";
class Style extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
     <Fragment>
         <h1 className="textColor"></h1>
     </Fragment>
    );
  }
}
export default Style;
Copy after login

to create a new .css file and import the file. If you use className="textColor" in the tag, you can use the style with the class 'textColor' in the imported .css file. This method is sufficient for general projects.

3. Use classnames to dynamically modify the style

import React, { Fragment } from "react";
import classNames from &#39;classnames&#39;
class Style extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
     <Fragment>
         <h1 className={classNames(&#39;textColor&#39;, {&#39;textContent&#39;: false} ,{&#39;textTitle&#39;: true})}></h1>
     </Fragment>
    );
  }
}
export default Style;
Copy after login

This method of dynamically modifying the style requires installing the plug-in classnames. In the above code, the classes of the h1 tag include textColor and textTitle. This is generally the case in projects. Use.

4. Use the styled-components plug-in to write the label style.

import React, { Fragment } from &#39;react&#39;
import styled from &#39;styled-components&#39;
const Title = styled.h1`
  color: #f00;
`
class Style extends React.Component {
  constructor(props) {
    super(props)
  }
  render() {
    return (
      <Fragment>
        <Title>复习style</Title>
      </Fragment>
    )
  }
}
export default Style
Copy after login

Use styled-components to write the label style. You need to install it first. Plug-in. The above code defines a Title, sets the style for the h1 tag through styled, and then the Title used in the component is equivalent to the styled h1 tag. This method is more common in large projects.

Related free learning recommendations: javascript(Video)

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template