美文网首页
Portal2版

Portal2版

作者: 立陶宛_d5a9 | 来源:发表于2019-12-24 22:52 被阅读0次
    import React, { Component } from "react";
    import { Text, View, Button, Dimensions } from "react-native";
    const ThemeContext = React.createContext({
      add: () => {}
    });
    class Portal extends Component {
      render() {
        return (
          <ThemeContext.Consumer>
            {({add}) => {
              setTimeout(()=>{
                add(this.props.children)
              },0)  
            }}
          </ThemeContext.Consumer>
        );
      }
    }
    let triggerChange = () => {}
    class Layer extends Component {
      state = {
        node: null,
        nub: 0
      }
      componentWillMount() {
        triggerChange = (node) => {
          this.setState({
            node
          })
        }
      }
      render() {
        return (<View style={{height: Dimensions.get("window").height - 100, backgroundColor: 'red', justifyContent: 'center', alignItems: 'center'}}>
          {this.state.node}
        </View>)
      }
    }
    class App extends Component {
      state = {
        node: null,
        nub: 0
      }
      add = (node) => {
        triggerChange(node)
      }
      addNub = () => {
        this.setState({
          nub: this.state.nub+1
        })
      }
      render() {
        return (
          <ThemeContext.Provider value={{add: this.add}}>
            <View style={{height: 100, backgroundColor: 'yellow'}}>
              <Text style={{textAlign: 'center'}}>本来该出现的位置</Text>
              <Portal>
                <Text>{this.state.nub}</Text>
                <View style={{height: 200, width: 200, backgroundColor: 'blue'}}>
                  <Text>蒙层</Text>
                </View>
                <Button onPress={this.addNub} title='CLICK' ></Button>
              </Portal>
            </View>
            <Layer></Layer>
          </ThemeContext.Provider>
        );
      }
    }
    
    export default App;
    

    相关文章

      网友评论

          本文标题:Portal2版

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