react-native彈窗封裝的方法
本文實例為大家分享了react-native彈窗封裝的具體代碼,供大家參考,具體內容如下
上圖
仿蘋果彈窗組件(android+ios均可用)
以上效果均基于本文的彈窗組件,后續(xù)將會介紹上面的組件,也可基于改組件定制更多組件
安裝依賴 yarn add react-native-root-siblings 或者 npm install react-native-root-siblings --save
主要代碼
顯示彈窗
export const showModal = (component) => { ? ? sibling = new RootSiblings(component); };
銷毀彈窗
export const destroySibling = (component) => ?sibling && sibling.destroy()
更新彈窗
export const update = (index, component) => sibling && sibling.update(<View>{component}</View>)
完整代碼
多彈窗管理不涉及,暫時介紹單個彈窗,感興趣的可以自己試試,將sibling改為數(shù)組;
//ShowModal.js import React from 'react'; import {View} from 'react-native'; import RootSiblings from 'react-native-root-siblings'; ?//全局彈框組件 let sibling = null; export const showModal = (component) => { ? ? sibling = new RootSiblings(component); }; export const destroySibling = (component) => ?sibling && sibling.destroy() export const update = (index, component) => sibling && sibling.update(<View>{component}</View>)
使用示例—>淡入背景
組件 ModalBg.js
import React from 'react'; import {Animated, InteractionManager, Easing, TouchableOpacity} from 'react-native'; import {getScreenHeight, getScreenWidth} from '../../utils/util'; import {destroyLastSibling} from '../showModal/ShowModal'; export default class ModalBg extends React.Component { ? animated = new Animated.Value(0); ? isShow = false; ? componentDidMount(): void { ? ? InteractionManager.runAfterInteractions(() => { ? ? ? this.handleAni(); ? ? }); ? } ? componentWillUnmount(): void { ? ? InteractionManager.runAfterInteractions(() => { ? ? ? this.handleAni(); ? ? }); ? } ? handleAni = () => { ? ? Animated.timing(this.animated, { ? ? ? toValue: this.isShow ? 0 : 1, ? ? ? duration: 250, ? ? ? easing: Easing.ease ? ? }).start(() => this.isShow = !this.isShow); ? }; ? render() { ? ? const opct = this.animated.interpolate({ ? ? ? inputRange: [0, 1], ? ? ? outputRange: [0, 0.4] ? ? }); ? ? return <Animated.View style={{ ? ? ? position: 'absolute', ? ? ? width: getScreenWidth(), ? ? ? height: getScreenHeight(), ? ? ? backgroundColor: '#000', ? ? ? opacity: opct, ? ? ? zIndex: 10 ? ? }}><TouchableOpacity onPress={() => { ? ? ? destroyLastSibling(); ? ? }} style={{flex: 1}} /></Animated.View>; ? } }
調用
showModal(<ModalBg />);
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
React.js組件實現(xiàn)拖拽排序組件功能過程解析
這篇文章主要介紹了React.js組件實現(xiàn)拖拽排序組件功能過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04使用 Rails API 構建一個 React 應用程序的詳細步驟
這篇文章主要介紹了使用 Rails API 構建一個 React 應用程序的詳細步驟,主要包括后端:Rails API部分,前端:React部分及React組件的相關操作,具有內容詳情跟隨小編一起看看吧2021-08-08React項目配置axios和反向代理和process.env環(huán)境配置等問題
這篇文章主要介紹了React項目配置axios和反向代理和process.env環(huán)境配置等問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12