1

i use react-native and react-native-swiper

Which OS ?

Android

Version

Which versions are you using:

  • react-native-swiper v1.5.13?
  • react-native v0.51.0

Actual behaviour

I display a Modal with swiper inside it. I get a blank display, only the button of the swiper but not the content

Expected behaviour

To show the content of the swiper inside a modal

How to reproduce it>

I tryed to reproduce the bug with a very simplified version of my code

try it in snack https://snack.expo.io/rk8rb3ZzM

or my code

        import React, { Component } from "react";
import { Text, View, Modal, Dimensions } from "react-native";
import Swiper from "react-native-swiper"; // 1.5.13
import { Button } from "react-native-elements"; // 0.18.5

import "@expo/vector-icons"; // 6.2.1

var width = Dimensions.get("window").width;
var height = Dimensions.get("window").height;

export default class extends Component {
  constructor(props) {
    super(props);
    this.state = {
      items:[
        { title: "Hello Swiper", css: thisstyles.slide1 },
        { title: "Beautiful", css: thisstyles.slide2 },
        { title: "simple", css: thisstyles.slide3 },
        { title: "And simple", css: thisstyles.slide3 }
      ],
      modalVisible: false
    };
  }
  componentDidMount() {

  }
  // affiche / cache la modal avec l'image en plein écran
  toggleModalVisible = () => {
    this.setState({ modalVisible: !this.state.modalVisible });
  };

  // vue plein écran avec zoom sans info
  renderFullScreen() {
    if (!this.state.modalVisible) {
      return null;
    }
    return (
      <Modal
        animationType={"slide"}
        transparent={false}
        visible={this.state.modalVisible}
        onRequestClose={() => this.toggleModalVisible()}
      >
        <View style={thisstyles.modalFullScreen}>
          <Text>I have component and text here</Text>
          <Swiper style={thisstyles.wrapper} showsButtons={true}>
            {this.state.items.map((item, key) => {
              console.log("item", item);
              return (
                <View key={key} style={item.css}>
                  <Text style={thisstyles.text}>{item.title}</Text>
                </View>
              );
            })}
          </Swiper>
        </View>
      </Modal>
    );
  }

  render() {
    return (
       <Swiper style={thisstyles.wrapper} showsButtons={true}>
            {this.state.items.map((item, key) => {
              console.log("item", item);
              return (
                <View key={key} style={item.css}>
                  <Text style={thisstyles.text}>{item.title}</Text>
                  {this.renderFullScreen()}
                    <Button
                      title="modal"
                      onPress={() => this.toggleModalVisible()}
                      icon={{ name: "close", type: "font-awesome" }}
                    />
                </View>
              );
            })}
          </Swiper>

    );
  }
}

const thisstyles = {
  modalFullScreen: {
    height: height,
    width: width
  },
  wrapper: {},
  slide1: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "#9DD6EB"
  },

  slide2: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "#97CAE5"
  },

  slide3: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "#92BBD9"
  },

  text: {
    color: "black",
    fontSize: 30,
    fontWeight: "bold"
  }
};

Steps to reproduce

  1. run the app
  2. click on the button "x modal"
AlainIb
  • 4,544
  • 4
  • 38
  • 64
  • Actually your snack code is different to the code you have here. And it works fine also – ShaneG Dec 15 '17 at 16:46
  • In your snack you have the slider items in state and it works fine on both platforms. Here you have your slider items in componentDidMount instead. Is it possible this could be the issue? – ShaneG Dec 15 '17 at 16:54
  • the state is not related to the issue because when you click on "x modal" button the component is already loaded – AlainIb Dec 15 '17 at 20:33
  • i edited the code and new link for snack. the first swipper work, but when you click the "x modal" the same code didn't work because in the modal the items title are not displayed ("Hello Swiper" etc), – AlainIb Dec 15 '17 at 20:34
  • I was looking at this at the weekend and i noticed the issue, but i am not sure how to fix it. I changed the slides for the modal so i could see it change text. The minute i clicked the modal button, the correct slider pops up, but a second one also pops up which is white and blank, which blocks the right one. Maybe its being called twice, but so far thats what i have seen – ShaneG Dec 18 '17 at 10:55
  • thanks. i don't found a solution for this bug. it worked well before (i used it since one month with same version ), maybe i updated another package who make this bug . I migred to react-native-looped-carousel – AlainIb Dec 18 '17 at 11:50

2 Answers2

2

I had to add a delay to get mine to work, but yes it works!

  constructor(props) {
    super(props);
    this.state = { showSwiper: false };
  }

  componentDidMount() {
    // Must use this 100-ms delayed swiper workaround to render on Android properly
    setTimeout(() => {
      this.setState({showSwiper: true});
    }, 100);
  }

  render() {
    var exampleSwiper = (
      <Swiper activeDotColor={'white'} loop={false} >
        <View style={{width: 100, height: 100, backgroundColor: 'white'}} />
        <View style={{width: 100, height: 100, backgroundColor: 'white'}} />
        <View style={{width: 100, height: 100, backgroundColor: 'white'}} />
      </Swiper>
    );
    return (
      <Modal presentationStyle={'overFullScreen'}>
        {this.state.showSwiper ? exampleSwiper : null}
      </Modal>
    );
  }
Bhargav Patel
  • 186
  • 1
  • 8
  • how is that exactly works? Do you have any idea? Mine also have the same issue, the difference is mine has image inside of each content of swiper. Stuck with the same issue for weeks :( – heisenberg Nov 19 '18 at 08:36
1

Create one parent View tag and set its height based on your requirement. For example:

<View style={{height:'50%'}}>
    <Swiper {....}    />
</View>
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Selva Kd
  • 21
  • 2