欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

react-native封裝插件swiper的使用方法

 更新時間:2018年03月20日 10:28:58   作者:sunshinezhong  
這篇文章主要介紹了react-native封裝插件swiper的使用方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

首先創(chuàng)建簡單的react-native項目,創(chuàng)建一個文件夾。然后用命令符輸入

react-native init swiper

創(chuàng)建完成之后開發(fā)項目,我用的vs

打開控制臺,安裝swiper依賴。

安裝:npm i react-native-swiper --save
查看:npm view react-native-swiper
刪除:npm rm react-native-swiper --save
這里還需要 npm i 下更新下本地的依賴庫

啟動app項目

ios: react-native run-ios
android: react-native run-android

開始上碼,在src里面創(chuàng)建個components文件夾下邊創(chuàng)建個swiper.js文件,以及index.js,加上說明文檔

import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { StyleSheet, TouchableWithoutFeedback, View } from 'react-native';
import RNSwiper from 'react-native-swiper';

const styles = StyleSheet.create({
 activeDotWrapperStyle: {
  //圓點樣式
 },
 activeDotStyle: {
  //圓點樣式
 },
 dotStyle: {
  //圓點樣式
 }
});

const activeDot = (
 <View style={styles.activeDotWrapperStyle}>
  <View style={styles.activeDotStyle} />
 </View>
);
const dot = <View style={styles.dotStyle} />;

export class Carousel extends Component {
 // Define component prop list
 static propTypes = {
  data: PropTypes.array,
  height: PropTypes.number,
  onPressItem: PropTypes.func,
  renderItem: PropTypes.func.isRequired,
  autoplay: PropTypes.bool,
  autoplayTimeout: PropTypes.number
 };

 // Define props default value
 static defaultProps = {
  data: [],
  height: 150,
  autoplay: true,
  autoplayTimeout: 2.5,
  onPressItem: () => {},
  renderItem: () => {}
 };

 // Define inner state
 state = {
  showSwiper: false
 };

 constructor(props) {
  super(props);
  this.handleItemPress = this.handleItemPress.bind(this);
 }

 componentDidMount() {
  setTimeout(() => {
   this.setState({ showSwiper: true });
  });
 }

 handleItemPress(item) {
  this.props.onPressItem(item);
 }

 _renderSwiperItem(item, index) {
  return (
   <TouchableWithoutFeedback key={index} onPress={() => this.handleItemPress(item)}>
    <View style={[{ flex: 1 }]}>{this.props.renderItem(item)}</View>
   </TouchableWithoutFeedback>
  );
 }

 render() {
  return this.props.data.length === 0 || !this.state.showSwiper ? null : (
   <RNSwiper
    height={this.props.height} //圖片高度
    activeDot={activeDot} 
    dot={dot}
    style={{ backgroundColor: '#fff' }}
    autoplay={this.props.autoplay} //是否自動輪播
    autoplayTimeout={this.props.autoplayTimeout} //輪播秒數(shù)
   >
    {this.props.data.map((item, idx) => this._renderSwiperItem(item, idx))} //如果數(shù)據(jù)是個對象里面的數(shù)組加一個循環(huán)
   </RNSwiper>
  );
 }
}

這是index.js文件

import { Carousel } from './carousel/Carousel';

export { Carousel };

公共組件庫

這里用于放置與業(yè)務(wù)無關(guān)的公共組件。組件實現(xiàn)必須考慮靈活性,擴展性,不能包含具體的業(yè)務(wù)邏輯。

組件必須以 你做的業(yè)務(wù)命名 為前綴,如 TryCarousel.js 。每個組件必須單獨放在目錄中,目錄必須全小寫(中橫線分割),如 carousel/TryCarousel.js 。

一個基本的組件結(jié)構(gòu):

import PropTypes from 'prop-types';
import React, { Component } from 'react';

export class TryCarousel extends Component {
 // Define component prop list
 static propTypes = {};

 // Define props default value
 static defaultProps = {};

 // Define inner state
 state = {};

 constructor(props) {
  super(props);
 }

 // LifeCycle Hooks

 // Prototype Functions

 // Ensure the latest function is render
 render() {}
}

組件列表

carousel(輪播組件)

主要用于通用的圖片輪播,能夠提供點擊事件響應(yīng)。

Usage:

Props:

屬性 描述 類型 默認值
data Carousel數(shù)據(jù)源 Array -
height Carousel的高度 number 150
onPressItem 點擊Carousel Item的時候觸發(fā) fn -
renderItem 具體的渲染Item的方法,請參考FlatList fn -
autoplay 是否自動切換 bool true
autoplayTimeout Item自動切換的時間間隔(單位s) number 2.5

需要導入的地方

import { HigoCarousel } from '../../components';
<Carousel
      data={} //接受的數(shù)據(jù)
      onPressItem={} //點擊事件
      height={} //圖片高度
      autoplay={} //是否自動播放
      autoplayTimeout={} //過渡時間
      renderItem={item => {
       return <Image source={{ uri: item.imageSource }} style={{ flex: 1 }} />;
      }} //圖片
/>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • react進階教程之異常處理機制error?Boundaries

    react進階教程之異常處理機制error?Boundaries

    在react中一旦出錯,如果每個組件去處理出錯情況則比較麻煩,下面這篇文章主要給大家介紹了關(guān)于react進階教程之異常處理機制error?Boundaries的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-08-08
  • react-beautiful-dnd拖拽排序功能的實現(xiàn)過程

    react-beautiful-dnd拖拽排序功能的實現(xiàn)過程

    這篇文章主要介紹了react-beautiful-dnd拖拽排序功能的實現(xiàn)過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • react表單受控的實現(xiàn)方案

    react表單受控的實現(xiàn)方案

    數(shù)據(jù)的受控控制一直是react里的一個痛點,當我想要實現(xiàn)一個輸入框的受控控制時,我需要定義一個onChange和value,手動去實現(xiàn)數(shù)據(jù)的綁定,本文小編給大家介紹了react表單受控的實現(xiàn)方案,需要的朋友可以參考下
    2023-12-12
  • React項目如何使用Element的方法步驟

    React項目如何使用Element的方法步驟

    本文主要介紹了React項目如何使用Element的方法步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • React表單容器的通用解決方案

    React表單容器的通用解決方案

    本文主要介紹了React表單容器的通用解決方案,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 在?React?Native?中使用?CSS?Modules的配置方法

    在?React?Native?中使用?CSS?Modules的配置方法

    有些前端工程師希望也能像開發(fā) web 應(yīng)用那樣,使用 CSS Modules 來開發(fā) React Native,本文將介紹如何在 React Native 中使用 CSS Modules,需要的朋友可以參考下
    2022-08-08
  • React+ts實現(xiàn)二級聯(lián)動效果

    React+ts實現(xiàn)二級聯(lián)動效果

    這篇文章主要為大家詳細介紹了React+ts實現(xiàn)二級聯(lián)動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • React Native中WebView與html雙向通信遇到的坑

    React Native中WebView與html雙向通信遇到的坑

    這篇文章主要介紹了React Native中WebView與html雙向通信的一些問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2023-01-01
  • React用法之高階組件的用法詳解

    React用法之高階組件的用法詳解

    高階組件也就是我們常說的HOC,是React中用于復用組件邏輯的一種高級技巧。這篇文章主要通過一些示例帶大家學習一下高階組件的使用,希望對大家有所幫助
    2023-04-04
  • react顯示文件上傳進度的示例

    react顯示文件上傳進度的示例

    這篇文章主要介紹了react顯示文件上傳進度的示例,幫助大家更好的理解和學習使用react,感興趣的朋友可以了解下
    2021-04-04

最新評論