react-native彈窗封裝的方法
本文實例為大家分享了react-native彈窗封裝的具體代碼,供大家參考,具體內(nèi)容如下
上圖

仿蘋果彈窗組件(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>;
? }
}調(diào)用
showModal(<ModalBg />);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
react中使用forEach或map兩種方式遍歷數(shù)組
這篇文章主要介紹了react中使用forEach或map兩種方式遍歷數(shù)組,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
React實現(xiàn)createElement 和 cloneElement的區(qū)別
本文詳細(xì)介紹了React中React.createElement和React.cloneElement兩種方法的定義、用法、區(qū)別及適用場景,具有一定的參考價值,感興趣的可以了解一下2024-09-09
使用 React 和 Threejs 創(chuàng)建一個VR全景項目的過程詳解
這篇文章主要介紹了使用 React 和 Threejs 創(chuàng)建一個VR全景項目的過程詳解,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
React新擴展函數(shù)setState與lazyLoad及hook介紹
這篇文章主要介紹了React新擴展函數(shù)setState與lazyLoad及hook,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12

