高阶组件之属性代理

新组件类继承子React.component类,对传入的组件进行一系列操作,从而产生一个新的组件,达到增强组件的作用

  • 操作props
  • 访问ref
  • 抽取state
  • 封装组件

废话不多说,直接上代码:

Tabbar.js

import React, { Component } from "react";
import { Link } from "react-router-dom";
import "./index.less";
const tabbarArr = [
  {
    img: "icon-home",
    text: "首页",
    link: "/home"
  },
  {
    img: "icon-fenlei",
    text: "分类",
    link: "/category"
  },
  {
    img: "icon-shoutibao",
    text: "拼购",
    link: "/pingou"
  },
  {
    img: "icon-gouwuche",
    text: "购物车",
    link: "/car"
  },
  {
    img: "icon-yonghu",
    text: "我的",
    link: "/user"
  }
];
const Tabbar = WrappedComponent =>
  class Tabbar extends Component {
    constructor(props) {
      super(props);
      this.state = {
        activeIndex: 0
      };
    }
    componentDidMount() {
    }
    render() {
      const url = window.location.href;
      return (
        <div className="container">
          <div className="content">
            <WrappedComponent />
          </div>
          <div className="tabbar">
            <div className="tabbar-content">
              {tabbarArr.map((item, index) => {
                return (
                  <Link
                    to={item.link}
                    className={
                      "tabbar-item" +
                      (url.indexOf(item.link) > 0 ? " active" : "")
                    }
                    key={index}
                  >
                    <div className={"iconfont " + item.img} />
                    <div>{item.text}</div>
                  </Link>
                );
              })}
            </div>
          </div>
        </div>
      );
    }
  };

export default Tabbar;

Home.js

import React, { Component } from "react";
import Tabbar from "../components/Tabbar";
@Tabbar
class Home extends Component {
  render() {
    return (
      <div>
        <img
          src={require("../static/images/home.jpeg")}
          className="bg"
          alt="首页"
        />
      </div>
    );
  }
}
export default Home;

效果如图:
图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值