【问题标题】:react-native Swiper is blank inside a modalreact-native Swiper 在模态中是空白的
【发布时间】:2017-12-15 15:47:09
【问题描述】:

我使用 react-native 和 react-native-swiper

哪个操作系统?

安卓

版本

您使用的是哪个版本:

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

实际行为

我在里面显示了一个带有 swiper 的 Modal。我得到一个空白显示,只有滑动器的按钮而不是内容

预期行为

在模态框内显示滑动条的内容

如何重现它>

我尝试使用非常简化的代码版本来重现该错误

在零食中试试 https://snack.expo.io/rk8rb3ZzM

或者我的代码

        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"
  }
};

重现步骤

  1. 运行应用程序
  2. 点击“x modal”按钮

【问题讨论】:

  • 实际上您的零食代码与您这里的代码不同。它也可以正常工作
  • 在您的零食中,您的滑块项目处于状态,并且在两个平台上都可以正常工作。在这里,您在 componentDidMount 中有您的滑块项目。这可能是问题吗?
  • 状态与问题无关,因为当您单击“x modal”按钮时,组件已加载
  • 我编辑了零食的代码和新链接。第一个 swipper 工作,但是当您单击“x modal”时,相同的代码不起作用,因为在 modal 中不显示项目标题(“Hello Swiper”等),
  • 我周末在看这个,我注意到了这个问题,但我不知道如何解决它。我改变了模态的幻灯片,所以我可以看到它改变了文本。在我点击模态按钮的那一刻,正确的滑块弹出,但第二个也弹出,它是白色和空白的,它挡住了右边的。也许它被调用了两次,但到目前为止这就是我所看到的

标签: javascript react-native react-native-swiper


【解决方案1】:

我不得不添加延迟才能让我的工作,但它确实有效!

  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>
    );
  }

【讨论】:

  • 这到底是如何工作的?你有什么主意吗?我的也有同样的问题,不同的是我的每个内容里面都有图像。几个星期以来都被同样的问题困扰:(
【解决方案2】:

创建一个父View标签并根据您的要求设置其高度。例如:

<View style={{height:'50%'}}>
    <Swiper {....}    />
</View>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 2016-03-07
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多