美文网首页
React导航栏封装

React导航栏封装

作者: kevin5979 | 来源:发表于2020-09-25 21:31 被阅读0次

父子组件传值练习

效果图


image.png
import React, {Component} from "react";
import PropTypes from "prop-types"

// 子组件
class TabBarItem extends Component {

  render() {
    const {tab_bars, curr_index} = this.props
    return (
      <ul>
        {
          tab_bars.map((item, index) => {
            return (
              <li
                key={index}
                onClick={e => this.props.fItemClick(index)}
                className={curr_index === index ? "active" : null}
              >
                {item}</li>
            )
          })
        }
      </ul>
    )
  }
}

// props验证
TabBarItem.propTypes = {
  tab_bars: PropTypes.array,
  curr_index: PropTypes.number,
  fItemClick: PropTypes.func
}

// 父组件
export default class TabBar extends Component {
  constructor(props) {
    super(props);
    this.tab_bars = ["热销", "新上", "推荐"]
    this.state = {
      curr_index: 0,
      curr_title: "热销"
    }
  }

  fItemClick = (index) => {
    this.setState({
      curr_index: index,
      curr_title: this.tab_bars[index]
    })
  }

  render() {
    return (
      <div>
        {
          <TabBarItem
            tab_bars={this.tab_bars}
            curr_index={this.state.curr_index}
            fItemClick={index => this.fItemClick(index)}
          />
        }
        <p>{this.state.curr_title}</p>
      </div>
    )
  }
}

END

相关文章

  • React Native 导航栏

    开始搞一下React Native;刚开始接触 React Native,本着IOS封装的习惯,今天把导航栏封装了...

  • React导航栏封装

    父子组件传值练习 效果图

  • taro开发RN遇到的问题

    1.导航栏标题不居中 导航栏用的是react-navigation 2.18.3版本,react-navigati...

  • React-Native-顶部导航栏实现

    在先前底部导航栏的基础上添加顶部导航栏。版本环境:react-native:0.60.5 react-navi...

  • React Native封装导航栏组件

    前言 RN开发页面时,基本上每一个页面我们都需要有导航栏,但是ReactNative又不像iOS那样,只要控制器包...

  • 特大新闻

    导航库React Navigation 功能:1.跳转页面 2.底部导航栏 3.顶部导航栏...

  • 参考笔记

    导航库React Navigation 功能:1.跳转页面 2.底部导航栏 3.顶部导航栏...

  • 直播项目笔记(一)

    颜色封装 + ClOPageView + 瀑布流 搭建主题框架 导航栏布局 改变导航栏的颜色 改变状态栏的颜色 设...

  • React-Native 导航栏切换

    原本在项目中封装了一个导航栏,其实公用就可以了,由于项目需求,导航栏在多个页面的布局都不相同,重新封装了一个导航栏...

  • 2019-03-18 react-navigation 去除导航

    react-navigation 去除导航阴影(如有同学使用了headerTransparent使得导航栏背景颜色...

网友评论

      本文标题:React导航栏封装

      本文链接:https://www.haomeiwen.com/subject/qsiuhktx.html