鍍金池/ 問答/HTML/ 表格輪詢組件,這段代碼怎么優(yōu)化一下,把公共部分提取出來?

表格輪詢組件,這段代碼怎么優(yōu)化一下,把公共部分提取出來?

import React from 'react'
import './styles/'
import Red from './images/red.png'
import Blue from './images/blue.png'
import Yellow from './images/yellow.png'

class Table extends React.Component {

    constructor() {
        super();
        this.state = {
            tableHead: [],
            tableBody: []
        }
        this.tableTimer = null;
    }

    render() {

        let backgroundColor = ['rgba(0,0,0,0)','rgba(42,244,255,0.2)'];

        let tdStyle = {
            height:40,
            fontSize: 20,
            color: 'rgba(255,255,255,0.6)',
        };
        let tdStyleOdd = {
            ...tdStyle,
            backgroundColor: backgroundColor[1],
        };
        let tdStyleEven = {
            ...tdStyle,
            backgroundColor: backgroundColor[0],
        };

        // 表頭通用樣式
        let thStyle = {
            textAlign: 'center',
            fontSize: 20,
            height:32,
            color: 'rgba(42,244,255,1)',
            backgroundColor: 'rgba(39,147,252,0.3)'
        }

        let emptyTdStyle = {
            width: 500,
            height:300,
            fontSize: 30,
            //backgroundColor: 'rgba(255,255,255,1)',
            textAlign: 'center',
            color:'rgba(255,255,255,0.5)'
        }

        let imgSmall = {
            width: 18,
            height:18
        }

        let emptyContent = (
            <tr>
                <td colSpan={6}  // 跨列
                    style={emptyTdStyle}>當(dāng)前無告警</td>
            </tr>
        );

        let tableHead = this.state.tableHead&&this.state.tableHead.length !==0
            &&this.state.tableHead.map(function (head,index) {
                if(head == 'index'){
                    //return '序號';
                    return (<th style= {{
                        ...thStyle,
                        width:70
                    }} key={index}>序號</th>)
                } else if (head == 'type') {
                    //return '任務(wù)名稱';
                    return (<th style= {{
                        ...thStyle,
                        width:180
                    }} key={index}>任務(wù)名稱</th>)
                } else if (head == 'level') {
                    //return '告警等級';
                    return (<th style= {{
                        ...thStyle,
                        width:80
                    }} key={index}>告警等級</th>)
                } else if (head == 'message') {
                    //return '告警信息';
                    return (<th style= {{
                        ...thStyle,
                        width:237
                    }} key={index}>告警信息</th>)
                }
            })

        const showCell = function(cell,index) {
            //
        }

        let tableBody = this.state.tableBody && this.state.tableBody.length != 0&&
            this.state.tableBody.map((row,index)=>{
                return (
                    <tr key={index} style={{

                    }}>
                        {index%2 == 0?
                            row.map((cell,index)=> {
                                if(cell=='故障'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleEven,

                                        }}
                                    ><img src={Red} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else if(cell=='正常'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleEven,

                                        }}
                                    ><img src={Blue} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else if(cell=='告警'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleEven,

                                        }}
                                    ><img src={Yellow} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else {
                                    if(index == 0){
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleEven,
                                                textAlign: 'center',
                                                width: 70
                                            }}
                                        >{cell}</td>
                                    } else if(index == 1){
                                        if(cell.length>=10){cell = cell.substring(0,5)+'...'}
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleEven,
                                                textAlign: 'center',
                                                width: 180
                                            }}
                                        >{cell}</td>
                                    } else if (index == 2) {
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleEven,
                                                textAlign: 'center',
                                                width: 80
                                            }}
                                        >{cell}</td>
                                    } else if (index == 3) {
                                        if(cell.length>=10){cell = cell.substring(0,10)+'...'}
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleEven,
                                                textAlign: 'center',
                                                width: 237
                                            }}
                                        >{cell}</td>
                                    }
                                }
                            }):
                            row.map((cell,index)=>{
                                if(cell=='故障'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleOdd
                                        }}
                                    ><img src={Red} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else if(cell=='正常'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleOdd
                                        }}
                                    ><img src={Blue} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else if(cell=='告警'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleOdd
                                        }}
                                    ><img src={Yellow} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else {
                                    if(index == 0){
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleOdd,
                                                textAlign: 'center',
                                                width: 70
                                            }}
                                        >{cell}</td>
                                    } else if(index == 1){
                                        if(cell.length>=10){cell = cell.substring(0,5)+'...'}
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleOdd,
                                                textAlign: 'center',
                                                width: 180
                                            }}
                                        >{cell}</td>
                                    } else if (index == 2) {
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleOdd,
                                                textAlign: 'center',
                                                width: 80
                                            }}
                                        >{cell}</td>
                                    } else if (index == 3) {
                                        if(cell.length>=10){cell = cell.substring(0,10)+'...'}
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleOdd,
                                                textAlign: 'center',
                                                width: 237
                                            }}
                                        >{cell}</td>
                                    }

                                }

                            })
                        }
                    </tr>
                )
            })

        return (
            <div>
                <table className={'telecommunication_table'}
                       border={0}
                       cellSpacing = {0}
                       cellPadding = {0}>
                    <thead className={'theads'}>
                        <tr>
                            {this.state.tableHead&&this.state.tableHead.length !==0?
                                tableHead: ''
                            }
                        </tr>
                    </thead>
                    <tbody>
                        {this.state.tableBody && this.state.tableBody.length != 0?
                            tableBody: emptyContent
                        }
                    </tbody>
                </table>
            </div>
        )
    }

    separateBody(tableBody,separateLength){

        let tableBodyList = [];
        let tableTempBody = [];

        tableBody&&tableBody.length!=0&&tableBody.map(function(item,index){

            tableTempBody.push(item)
            if(tableTempBody.length == separateLength){

                tableBodyList.push(tableTempBody);
                tableTempBody = [];
            } else if (index == tableBody.length-1) {
                tableBodyList.push(tableTempBody);
                tableTempBody = [];
            }
        })

        return tableBodyList;
    }

    componentDidMount() {

        const _self = this;
        let tableBody = [];
        let tableHead = ['index','type','level','message'];

        let separateLength = 10;
        let tableSeparateData = _self.separateBody(tableBody,separateLength)
        //console.log('tableSeparateData =', tableSeparateData);
        this.stateChange(tableHead,tableSeparateData[0]);
        if(tableSeparateData&&tableSeparateData.length>1){
            _self.tableTimer = _self._setInterval(tableSeparateData);
        }

    }

    _setInterval(param){
        const _self= this;
        let table_index = 1;
        var set_interval= setInterval(function(){
            _self.setState({
                tableBody: param[table_index]
            });
            if( table_index+1 == param.length){
                table_index = 0;
            }else {
                table_index = table_index+1;
            }

        },5000)

        return set_interval;
    }

    stateChange(head, body){
        //console.log(receiveData);
        const _self= this;
        _self.setState({
            tableHead: head,
            tableBody: body
        })
    }

    tableBodyChange(){
        console.log('tableBody ==', this.state.tableBody)
    }

    componentWillReceiveProps(nextProps) {
        const _self= this;
        //console.log('組件將被更新')
        if(_self.tableTimer != null){
            window.clearInterval(_self.tableTimer)
        }
        let separateLength = 10;
        let tableSeparateData = _self.separateBody(nextProps.tableBody,separateLength)

        this.stateChange(nextProps.tableHead,tableSeparateData[0]);
        if(tableSeparateData&&tableSeparateData.length>1){
            _self.tableTimer = _self._setInterval(tableSeparateData);
        }
    }

    componentWillUnmount() {
        //console.log('組件將被移除')
        if(this.tableTimer != null){
            window.clearInterval(this.tableTimer)
        }

    }
}

export default Table;

我想把下面這段優(yōu)化一下,請問如何處理?

let tableBody = this.state.tableBody && this.state.tableBody.length != 0&&
            this.state.tableBody.map((row,index)=>{
                return (
                    <tr key={index} style={{

                    }}>
                        {index%2 == 0?
                            row.map((cell,index)=> {
                                if(cell=='故障'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleEven,

                                        }}
                                    ><img src={Red} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else if(cell=='正常'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleEven,

                                        }}
                                    ><img src={Blue} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else if(cell=='告警'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleEven,

                                        }}
                                    ><img src={Yellow} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else {
                                    if(index == 0){
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleEven,
                                                textAlign: 'center',
                                                width: 70
                                            }}
                                        >{cell}</td>
                                    } else if(index == 1){
                                        if(cell.length>=10){cell = cell.substring(0,5)+'...'}
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleEven,
                                                textAlign: 'center',
                                                width: 180
                                            }}
                                        >{cell}</td>
                                    } else if (index == 2) {
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleEven,
                                                textAlign: 'center',
                                                width: 80
                                            }}
                                        >{cell}</td>
                                    } else if (index == 3) {
                                        if(cell.length>=10){cell = cell.substring(0,10)+'...'}
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleEven,
                                                textAlign: 'center',
                                                width: 237
                                            }}
                                        >{cell}</td>
                                    }
                                }
                            }):
                            row.map((cell,index)=>{
                                if(cell=='故障'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleOdd
                                        }}
                                    ><img src={Red} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else if(cell=='正常'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleOdd
                                        }}
                                    ><img src={Blue} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else if(cell=='告警'){
                                    return <td
                                        className={'emeBodyTd'}
                                        style={{
                                            ...tdStyleOdd
                                        }}
                                    ><img src={Yellow} style={{
                                        ...imgSmall
                                    }}/>{cell}</td>
                                } else {
                                    if(index == 0){
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleOdd,
                                                textAlign: 'center',
                                                width: 70
                                            }}
                                        >{cell}</td>
                                    } else if(index == 1){
                                        if(cell.length>=10){cell = cell.substring(0,5)+'...'}
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleOdd,
                                                textAlign: 'center',
                                                width: 180
                                            }}
                                        >{cell}</td>
                                    } else if (index == 2) {
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleOdd,
                                                textAlign: 'center',
                                                width: 80
                                            }}
                                        >{cell}</td>
                                    } else if (index == 3) {
                                        if(cell.length>=10){cell = cell.substring(0,10)+'...'}
                                        return <td
                                            className={'emeBodyTd'}
                                            style={{
                                                ...tdStyleOdd,
                                                textAlign: 'center',
                                                width: 237
                                            }}
                                        >{cell}</td>
                                    }

                                }

                            })
                        }
                    </tr>
                )
            })
回答
編輯回答
好難瘦
let tableBody = this.state.tableBody && this.state.tableBody.length != 0 &&
    this.state.tableBody.map((row,index)=>{
        return (
            <tr key={index} style={{ }}>{
                row.map((cell,i)=>{
                    if(['故障','正常','告警'].includes(cell)){
                        return <td
                            className={'emeBodyTd'}
                            style={{
                                index%2==0?...tdStyleEven:...tdStyleOdd,
                            }}
                        ><img src={cell=='正常'?Blue:cell=='故障'?Red:Yellow} style={{
                            ...imgSmall
                        }}/>{cell}</td>
                    } else {
                        const styles = [70,180,80,237];
                        return <td
                            className={'emeBodyTd'}
                            style={{
                                index%2==0?...tdStyleEven:...tdStyleOdd,
                                textAlign: 'center',
                                width: styles[i]
                            }}
                        >{cell}</td>
                    }
                })
            }</tr>
        )
    })
2017年6月4日 20:50