In the React project, a piece of code is optimized.

import React from "react";
import ReactDOM from "react-dom";
import "./assets/styles/index.less";

import Green from "./assets/images/green.png";
import Red from "./assets/images/red.png";

class Circle extends React.Component {

    constructor() {
        super();
        this.state = {
            safety: []
        }
    }

    render() {

        const _self = this;

        //console.log("_self.props.styleSet =", _self.props.styleSet)
        let divStyle = {
            //
        }

        let safetyData = {};



        if(this.state.safety.length !== 0) {
            this.state.safety.map(function (item) {
                safetyData[item["name"]] = item["value"]
            })
        }



        let cI = Red


        return (
                <div
                    ref={(elem) => { this.element = elem; }}
                    className={"safety_div"}
                >
                    <div className={"safety_title"}>
                        <div className={"item item1"}></div>
                        <div className={"item item2"}></div>
                        <div className={"item item3"}></div>
                        <div className={"item item4"}></div>
                    </div>


                    <div className={"safety_img"}>
                        <div
                            className={"item item1"}
                        >
                            <img src={safetyData["handle"] === 0?Red:Green} />
                        </div>
                        <div
                            className={"item item1"}
                        >
                            <img src={safetyData["configuration"] === 0?Red:Green} />
                        </div>
                        <div
                            className={"item item1"}
                        >
                            <img src={safetyData["violentBreak"] === 0?Red:Green} />
                        </div>
                        <div
                            className={"item item1"}
                        >
                            <img src={safetyData["weakPassword"] === 0?Red:Green} />
                        </div>
                    </div>

                    <div className={"safety_figure"}>
                        <div
                            className={"item item1"}
                            style = {{
                                color: safetyData["handle"] === 0?"-sharpFF2366":"-sharp2AF4FF"
                            }}
                        >
                            {safetyData["handle"]}
                        </div>
                        <div
                            className={"item item2"}
                            style = {{
                                color: safetyData["configuration"] === 0?"-sharpFF2366":"-sharp2AF4FF"
                            }}
                        >
                            {safetyData["configuration"]}
                        </div>
                        <div
                            className={"item item3"}
                            style = {{
                                color: safetyData["violentBreak"] === 0?"-sharpFF2366":"-sharp2AF4FF"
                            }}
                        >
                            {safetyData["violentBreak"]}
                        </div>
                        <div
                            className={"item item4"}
                            style = {{
                                color: safetyData["weakPassword"] === 0?"-sharpFF2366":"-sharp2AF4FF"
                            }}
                        >
                            {safetyData["weakPassword"]}
                        </div>
                    </div>

                </div>

        )
    }

    componentDidMount() {
        //
    }

    componentWillReceiveProps(nextProps) {  // 
        //console.log(nextProps.data);
        this.setState({
            safety: nextProps.safety
        });
    }

    componentDidUpdate() {
        //
    }
}


export default Circle;

Feb.26,2021

you probably need to optimize the repetitive div .

get computedSafetyDate() {
    ...
    }
  1. reduce unnecessary update judgments for components
  2. reduce the use of bind, arrow functions within components (because these recreate a function in js and degrade performance)

that's all for the time being

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b161c3-2a53c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b161c3-2a53c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?