import React, { Component } from "react";
import {
Text,
DeviceEventEmitter,
Button,
View,
Dimensions
} from "react-native";
const w_height = Dimensions.get("window").height
class PortalHost extends Component {
state = {
nodeList: {},
}
id: number = 0
componentWillMount() {
DeviceEventEmitter.addListener("addType", ({node, id}) => {
const { nodeList } = this.state;
this.setState({
nodeList: {
...nodeList,
[id]: node
}
});
});
DeviceEventEmitter.addListener("removeType", id => {
const { nodeList } = this.state;
delete nodeList[id];
this.setState({
nodeList
});
});
}
render() {
const { nodeList } = this.state;
return (
<View style={{ flex: 1 }}>
<View>{this.props.children}</View>
{Object.keys(nodeList).map((key)=>{
return (
<View
style={{
position: "absolute",
top: 0,
left: 0,
right: 0
}}
>
{nodeList[key]}
</View>
);
})}
{/* <View style={{position: 'absolute', top: 0, left: 0, right: 0}}>{node}</View> */}
</View>
);
}
}
class Portal extends Component {
static id = 0;
static Host = PortalHost;
static add = (node: JSX.Element) => {
Portal.id++;
DeviceEventEmitter.emit("addType", { node, id: Portal.id });
return Portal.id;
};
static remove = (id: number) => {
DeviceEventEmitter.emit("removeType", id);
};
render() {
return null;
}
}
class App extends Component {
render() {
return (
<View style={{ height: w_height }}>
<Portal.Host>
<View style={{ paddingTop: 60 }}>
<Button
title="点击"
onPress={() => {
const id = Portal.add(
<View
style={{
height: w_height,
backgroundColor:
"rgba(0, 0, 0, 0.5)",
justifyContent: "center",
alignItems: "center"
}}
onTouchStart={()=>{
Portal.remove(id);
}}
>
<Text>弹出层</Text>
</View>
);
}}
/>
</View>
</Portal.Host>
</View>
);
}
}
export default App;
Portal简单版
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 今天是我们个人成长集训营的第三天——断舍离日。 作业内容如下 简单版:收拾一下你的办公桌,让你的桌上,留尽量少的工...
- 先来准备下素材 找一个模特,或者是自己、朋友、恋人都可以,尽量找图大一些的。 Step 1 给模特进行抠图 Ste...