代码之家  ›  专栏  ›  技术社区  ›  jaycodez

如何重构此代码以减少重复?

  •  -1
  • jaycodez  · 技术社区  · 4 年前
        constructor(component) {
            this.component = component;
            utils.REACT.bindClassToMethods(this, [
                'clearExtended',
                'searchPrice',
                'searchAddress',
                'searchExtended',
                'expandSearchInit',
                'allSubTypes',
                'initCustomCheckbox',
                'updateAddressFromAutocomplete',
                'onChangeOrganisationId'
            ]);
    
            var sPageURL = window.location.search.substring(1),
                sURLVariables = sPageURL.split('&'),
                sParameterName,
                i;
            for (i = 0; i < sURLVariables.length; i++) {
                sParameterName = sURLVariables[i].split('=');
                if (sParameterName[0] === 'filter' && sParameterName[1] === 'sold') {
                    this.component.setState({
                        defaultFilter: true,
                        searchQuery: 'sold'
                    });
                }
                if (sParameterName[0] === 'filter' && sParameterName[1] === 'leased') {
                    this.component.setState({
                        defaultFilter: true,
                        searchQuery: 'leased'
                    });
                }
            }
            {
                (component.state.defaultFilter === true && component.state.searchQuery === 'sold')
                    ? this.component.setState({ defaultValue: ["Sold"] })
                    : (component.props.is_tab !== undefined && component.props.is_tab === true)
                        ? this.component.setState({ defaultValue: ["Active", "Current", "Under Offer", "Sold", "Off Market", "Withdrawn", "Leased", "Draft"] })
                        : component.props.admin.length > 0 ? this.component.setState({ defaultValue: ["Active", "Current", "Under Offer"] }) : this.component.setState({ defaultValue: ["Active", "Current", "Under Offer", "Draft"] })
            }
            {
                (component.state.defaultFilter === true && component.state.searchQuery === 'leased')
                    ? this.component.setState({ defaultValue: ["Leased"] })
                    : (component.props.is_tab !== undefined && component.props.is_tab === true)
                        ? this.component.setState({ defaultValue: ["Active", "Current", "Under Offer", "Sold", "Off Market", "Withdrawn", "Leased", "Draft"] })
                        : component.props.admin.length > 0 ? this.component.setState({ defaultValue: ["Active", "Current", "Under Offer"] }) : this.component.setState({ defaultValue: ["Active", "Current", "Under Offer", "Draft"] })
            }
        }
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   Emre Koc    4 年前

    您可以做的第一件事是合并 setStates() :

    if (sParameterName[0] === 'filter' && (sParameterName[1] === 'sold' || sParameterName[1] === 'leased')) {
        this.component.setState({
            defaultFilter: true,
            searchQuery: sParameterName[1],
        });
    }
    
    // Better with array
    if (sParameterName[0] === 'filter' && ['sold', 'leased'].includes(sParameterName[1])) {
        this.component.setState({
            defaultFilter: true,
            searchQuery: sParameterName[1],
        });
    }
    

    虽然不完全确定for循环后的块做了什么,但也许你可以在那里使用相同的逻辑。