react-native-snap-carousel无法正确渲染

bzl*_*ght 5 javascript reactjs react-native react-native-snap-carousel

问题总结

我正在使用react-native-snap-carousel,并且渲染不正确。它仅在被刷过后才呈现,我需要在屏幕最初呈现时呈现。当我将屏幕分配给我的BottomTabNavigator的初始路径或React Navigation中我的Stack Navigator中的初始路径时,轮播会完美呈现。当我在Stack Navigator中将完全相同的屏幕分配给任何其他路线时,只有在我滑动轮播时,它才会渲染轮播。

我需要使用带有轮播的屏幕作为我的Stack Navigator中的第二条路线,但我不知道如何使其正常工作。

我尝试过的

  1. 我尝试将所有其他内容都移出屏幕
  2. 我也尝试过从头开始创建新屏幕。
  3. 我已经在Stack Navigator中将屏幕作为初始路径进行了测试,并且效果很好,但是当屏幕加载时,仍然无法使轮播呈现。
  4. 我也尝试过切换到基于类的react组件,但没有帮助。

旋转木马组件

import React, { useState } from "react";
import { View, Text } from "react-native";
import { useDispatch } from "react-redux";
import styles from "./StatSelectorStartComp.style";
import HeaderText from "~/app/Components/HeaderText/HeaderText";
import Carousel from "react-native-snap-carousel";
import LargeButton from "~/app/Components/Buttons/LargeButton/LargeButton";
import NavigationService from "~/app/services/NavigationService";
import { saveStartCompStatCategory } from "~/app/Redux/actions/dailyCompActions";

const StatSelectorStartComp = ({}) => {
  const dispatch = useDispatch();
  const ENTRIES1 = ["Kills", "Wins", "K/D", "Win %"];
  const [selectedStat, setSelectedStat] = useState(ENTRIES1[0]);

  const _renderItem = ({ item, index }) => {
    return (
      <View style={styles.slide}>
        <Text style={styles.compSelectStatCarousel}>{item}</Text>
      </View>
    );
  };

  return (
    <View style={styles.container}>
      <View style={styles.headerTextView}>
        <HeaderText header={"Configure Competition"} />
      </View>
      <Text style={styles.h5Secondary}> Which stat will you track?</Text>
      <View style={styles.selectStatView}>
        <Text style={styles.mediumGreyedOut}>Most {selectedStat} Wins</Text>
        <Carousel
          ref={c => {
            _carousel = c;
          }}
          data={ENTRIES1}
          renderItem={_renderItem}
          sliderWidth={375}
          itemWidth={100}
          onSnapToItem={index => {
            setSelectedStat(ENTRIES1[index]);
          }}
        />
      </View>
      <View style={styles.buttonView}>
        <LargeButton
          onPress={() => {
            dispatch(saveStartCompStatCategory(selectedStat));
            NavigationService.navigate("CompAddFriends");
          }}
          buttonText="Add Friends"
        />
      </View>
    </View>
  );
};

export default StatSelectorStartComp;
Run Code Online (Sandbox Code Playgroud)

旋转木马组件的样式

import { StyleSheet } from "react-native";
import { backgroundColor } from "~/app/Constants";
import {
  h5Secondary,
  mediumGreyedOut,
  compSelectStatCarousel
} from "~/app/FontConstants";

export default StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "space-between",
    backgroundColor
  },
  headerTextView: {
    flex: 1
  },
  h5Secondary,
  selectStatView: {
    flex: 3
  },
  mediumGreyedOut,
  compSelectStatCarousel,
  buttonView: {
    flex: 2
  }
});
Run Code Online (Sandbox Code Playgroud)

反应导航配置

const StartCompStack = createStackNavigator({
  StartFriendsComp: {
    screen: StartFriendsComp
  },
  StatSelectorStartComp: {
    screen: CarouselTest
  },
  CompAddFriends: {
    screen: CompAddFriends
  },
  FinalCompScreen: {
    screen: FinalCompScreen
  }
});

const ProfileStack = createStackNavigator({
  Profile: {
    screen: ProfileScreen
  },
  Settings: {
    screen: SettingsScreen
  }
});

const BottomTabNav = createBottomTabNavigator(
  {
    Home: {
      screen: HomeScreen
    },
    Competitions: {
      screen: Competitions
    },
    StartComp: {
      screen: StartCompStack,
      navigationOptions: () => ({
        tabBarVisible: false
      })
    },
    CompScreen: {
      screen: CompScreen
    },
    Friends: {
      screen: FriendsDrawer
    },
    Profile: {
      screen: ProfileStack
    },
    FacebookFriendsList
  },
  {
    tabBarComponent: CustomTabNav,
    initialRouteName: "Home" 
  }
);
Run Code Online (Sandbox Code Playgroud)

图片概述了问题

屏幕加载时,轮播未呈现 屏幕加载时,轮播未呈现

旋转轮播后 旋转轮播后

ron*_*ler 23

有一个实验性配置(当前为 v3.8.4) - removeClippedSubviews-true默认设置为。设置它来false解决我的问题。

我强烈建议不要使用延迟,因为它不是确定性的,并且每个设备都会发生变化。

感谢@auticcat 在评论中写道。


小智 7

同样的问题出现在我们的项目中,一个小技巧可以帮助我们。我们已将默认加载状态设置为 true 并在 componentDidMount 中将状态设置为 false 以显示 Carousel

试试这个,它可能会帮助你

state = { loading: true };

  componentDidMount() {
    setTimeout(() => {
      this.setState({ loading: false });
    }, 10);
  }
  render() {
    if(this.state.loading) {
      return null;
    }

    // return component render which contain Carousel
    ......... 
  }

Run Code Online (Sandbox Code Playgroud)

  • 我通过简单地使用“removeClippedSubviews={false}”解决了这个问题 (13认同)