react-location 高阶组件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了react-location 高阶组件相关的知识,希望对你有一定的参考价值。

/**
 * Created by EX-ZHANGLEI009 on 2017/11/1.
 */
import React from ‘react‘;
import { withRouter } from ‘react-router‘
import { method } from ‘./common‘;
@withRouter
export const locationUrl = (MyComponent) =>{
    return class LocationUrl extends React.Component{
        constructor(props){
            super(props);
        }
        get firstUrl(){
            const { params,location } = this.props;
            const isEmptyParams = method.isEmptyObj(params);
            if(isEmptyParams){
                return location.pathname;
            }
            let url = location.pathname;
            Object.keys(params).forEach(item =>{
                url = url.replace(`/${params[item]}`,‘‘);
            });
            return url;
        }
        get secondUrl(){
            const { params,location } = this.props;
            const isEmptyParams = method.isEmptyObj(params);
            if(isEmptyParams){
                return location.pathname;
            }
            const arr = Object.entries(params);
            if(arr.length === 1){
                return location.pathname.replace(`/${arr[0][1]}`,‘‘)
            }else{
                return location.pathname.replace(`/${arr[1][1]}`,‘‘)
            }
        }
        get thirdUrl(){
            const { params,location } = this.props;
            return location.pathname;
        }
        render(){
            const newProps = {
                firstUrl:this.firstUrl,
                secondUrl:this.secondUrl,
                thirdUrl:this.thirdUrl,
            };
            return <MyComponent {...newProps} {...this.props} />
        }
    }
};

 

以上是关于react-location 高阶组件的主要内容,如果未能解决你的问题,请参考以下文章

React高阶组件

React HOC(高阶组件)

高阶组件

React11.高阶组件

React11.高阶组件

FragmentContainer 与仅作为包装器的另一个 React 组件?