简体   繁体   English

尝试在 react-native-modal 中组合 2 个或多个模态

[英]Trying to combine 2 or multiple modals in react-native-modal

I've used react-native modals https://github.com/react-native-community/react-native-modal .我使用了 react-native 模态https://github.com/react-native-community/react-native-modal I'm trying to combine bottom half modal and modal slide from the sides using multiple modals.我正在尝试使用多个模态从侧面组合下半模态和模态幻灯片。 But while coming back from 2nd modal to 1st one, the modal goes down (closes) and then another opens.但是当从第二个模态返回到第一个模态时,模态下降(关闭)然后另一个打开。 Please have a look at the videos below to see what I wanted to do.请看看下面的视频,看看我想做什么。

What I'm trying to obtain with the modal https://youtu.be/YaMjp_VJ_9Y我想通过模式https://youtu.be/YaMjp_VJ_9Y获得什么

what is happening using react-native-modal https://youtu.be/GR8otXHhElc使用 react-native-modal 发生了什么https://youtu.be/GR8otXHhElc

Code代码

export default class App extends Component<Props> {
  state = {
    visibleModal: null
  };

  renderButton = (text, onPress) => (
    <TouchableOpacity onPress={onPress}>
      <View style={styles.button}>
        <Text>{text}</Text>
      </View>
    </TouchableOpacity>
  );

  renderModalContent = () => (
    <View style={styles.modalContent}>
      <Text>Hello!</Text>
      {this.renderButton("Next Modal", () =>
        this.setState({ visibleModal: 6 })
      )}
      {this.renderButton("Close", () => this.setState({ visibleModal: null }))}
    </View>
  );

  renderNextModalContent = () => (
    <View style={styles.modalContent}>
      <Text>Hello from next modal!</Text>
      {this.renderButton("BACK", () => this.setState({ visibleModal: 5 }))}
    </View>
  );

  render() {
    return (
      <View style={styles.container}>
        {this.renderButton("modal", () => this.setState({ visibleModal: 5 }))}
        <Modal
          isVisible={this.state.visibleModal === 5}
          onBackButtonPress={() => this.setState({ visibleModal: null })}
          style={styles.bottomModal}
        >
          {this.renderModalContent()}
        </Modal>
        <Modal
          isVisible={this.state.visibleModal === 6}
          animationIn="slideInLeft"
          animationOut="slideOutRight"
          onBackButtonPress={() => this.setState({ visibleModal: null })}
          style={styles.bottomModal}
        >
          {this.renderNextModalContent()}
        </Modal>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center"
  },
  bottomModal: {
    // flex: 0,
    justifyContent: "flex-end",
    margin: 0
  },
  button: {
    backgroundColor: "lightblue",
    padding: 12,
    margin: 16,
    justifyContent: "center",
    alignItems: "center",
    borderRadius: 4,
    borderColor: "rgba(0, 0, 0, 0.1)"
  },
  modalContent: {
    backgroundColor: "white",
    padding: 22,
    justifyContent: "flex-end",
    borderRadius: 4,
    borderColor: "rgba(0, 0, 0, 0.1)"
  }
});

I am afraid that modal should not be use in that way.恐怕不应该以这种方式使用模态。 From my perspective, what you are trying to archive can be done without using 2 modal从我的角度来看,您可以在不使用 2 模态的情况下完成您要归档的内容

My suggestion我的建议

  • Make a component that will mount when you call modal out制作一个组件,当您调用模态时将挂载
  • In the component you will make 2 views which you will add animation to the slidein view在组件中,您将制作 2 个视图,您将在滑入视图中添加动画
  • So, when you press the trigger, the other view will just slidein inside the same modal所以,当你按下触发器时,另一个视图只会在同一个模态中滑入

Hope this help!希望这有帮助!

I faced a similar issue in a project I had, that it bigger it got more and more modals were created in more and more screens and scenarios (without speaking on foreground notification etc... ),我在我的一个项目中遇到了类似的问题,它越大,在越来越多的屏幕和场景中创建了越来越多的模态(没有谈到前台通知等......),

So I ended up creating this package , for controlling all modals in my app with their own hierarchy所以我最终创建了这个,用于使用自己的层次结构控制我的应用程序中的所有模态

@idiosync/react-native-modal uses a hook interface, and doesn't use the additional native layer that react-native and react-native-modal implementations do. @idiosync/react-native-modal 使用钩子接口,并且不使用 react-native 和 react-native-modal 实现所做的附加本机层。

This means that new modals just appear on new layers so you can add as many as you like.这意味着新模态只会出现在新层上,因此您可以添加任意数量的模态。

You must ensure that you have an appropriate way to remove them from the component in question though!但是,您必须确保有适当的方法将它们从相关组件中删除!

https://www.npmjs.com/package/@idiosync/react-native-modal https://www.npmjs.com/package/@idiosync/react-native-modal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM