gpt4 book ai didi

javascript - React-native-snap-carousel 无法正确渲染

转载 作者:行者123 更新时间:2023-12-03 13:28:45 27 4
gpt4 key购买 nike

问题摘要

我正在使用react-native-snap-carousel,但它无法正确渲染。它仅在滑动后才呈现,我需要在屏幕最初呈现时呈现它。当我将屏幕分配给 BottomTabNavigator 的初始路线或 React Navigation 中 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;

带有轮播的组件的样式

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

react 导航配置

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

概述问题的图片

屏幕加载时,轮播未呈现 When screen loads, carousel not rendered

在轮播上滑动后 After swiping on carousel

最佳答案

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

我强烈建议不要使用延迟,因为它不确定并且会根据设备而变化。

感谢 @auticcat 在评论中写下此内容。

关于javascript - React-native-snap-carousel 无法正确渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57797241/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com