鍍金池/ 問答/HTML/ 初學(xué)react,這段代碼怎么優(yōu)化?

初學(xué)react,這段代碼怎么優(yōu)化?

renderFooter (quantity) {

        const _self = this;

        let wordStyle = {
            color: 'rgba(71,129,255,1)'
        }

        let numberFrameCommon = {
            width: 40,
            height: 40,
            marginLeft: 10,
            cursor:'pointer'
        }

        let numberFrame = {
            ...numberFrameCommon,
            background: 'rgba(71, 129, 255, 1)',
            color: 'rgba(255, 255, 255, 1)',
        }

        let numberFrame2 = {
            ...numberFrameCommon,
            background: 'rgba(27, 29, 45, 1)',
            color:'rgba(99, 107, 138, 1)'
        }


        let ellipsisStyle = {
            width:40,
            height:40,
            background: 'rgba(27,29,45,1)',
            marginLeft:10,
            fontSize:'14px',
            borderRadius: '2px',
            fontFamily: 'MicrosoftYaHei',
            color:'rgba(99,107,138,1)'
        }


        let data = [];

        for(let di=1; di<=_self.state.pageQuantity; di++) {
            data.push(di)
        }

        //console.log( 'pageQuantity = ', _self.state.pageQuantity )

        if ( _self.state.pageQuantity <= 10 ) {
            return (
                data.map((item, index, arr) => {

                    //console.log('item = ', item);
                    //console.log('index = ', index);

                    return (<div
                        key = {index}
                        style = {
                            _self.state.footFlag === index ?
                                numberFrame: numberFrame2
                        }
                        className={'numberFrame flexCenter'}
                        onClick={() => this.showPageData(index)}
                    >
                        {index+1}
                    </div>)

                })
            )
        }  else if ( _self.state.pageQuantity >10 ) {

            return (

                data.map((item, index, arr) => {

                    //console.log('item = ', item);
                    //console.log('index = ', index);
                    if(index<9) {
                        return (<div
                            key = {index}
                            style = {
                                _self.state.footFlag === index ?
                                    numberFrame: numberFrame2
                            }
                            className={'numberFrame flexCenter'}
                            onClick={() => this.showPageData(index)}
                        >
                            {index+1}
                        </div>)
                    } else if ( index === 9) {
                        return (<div
                            key = {index}
                            style = {ellipsisStyle}
                            className={'numberFrame'}
                        >
                            ...
                        </div>)
                    } else if (item === _self.state.pageQuantity) {
                        return (<div
                            key = {index}
                            style = {numberFrame}
                            className={'numberFrame'}
                            onClick={() => this.showPageData(index)}
                        >
                            {item}
                        </div>)
                    }
                })
            )

        }

    }
回答
編輯回答
遲月

邏輯應(yīng)該不需要怎么優(yōu)化,寫法可以優(yōu)化

  • 比如前半部分代碼剝離出去,單獨(dú)作為一個方法調(diào)用
  • 每個if else分支單獨(dú)寫一個方法調(diào)用
2018年1月21日 10:06
編輯回答
愚念
  • 抽離樣式
  • 不需要又用for,又用map,直接map即可。
2018年7月20日 06:07