【问题标题】:How to re-render React Native Web Swiper based on State change?如何根据状态变化重新渲染 React Native Web Swiper?
【发布时间】:2020-03-23 18:20:43
【问题描述】:

我正在使用 this 包在 React Native 中创建 Swiper。 我使用了静态数据并且它正在工作,但现在我正在从服务器获取数据并将其保存为Array 的状态。 问题是当在 swiper 内映射数据以创建动态滑块时,它没有显示任何内容,但如果我在 Swiper 之外执行它,它正在呈现。

我该如何解决这个问题?

 <Swiper
    from={0}
    minDistanceForAction={0.1}
    controlsProps={{
        dotsTouchable: true,
        prevPos: 'left',
        nextPos: 'right',
        nextTitle: '',
        prevTitle: '',
        dotsWrapperStyle: { marginTop: ScaleHelpers.CalcHeight(8), width: ScaleHelpers.CalcWidth(7), height: ScaleHelpers.CalcHeight(7), borderRadius: ScaleHelpers.CalcWidth(7) },
                                dotActiveStyle: { width: ScaleHelpers.CalcWidth(7), height: ScaleHelpers.CalcHeight(7), borderRadius: ScaleHelpers.CalcWidth(7) },
                            }}
 >

 {
   this.state.notifications.map((notif, index) => {
      return (
          <View style={[styless.slideContainer]}>

                                            <Card transparent style={{
                                                flex: 0,
                                                elevation: 3,
                                                shadowColor: "#000",
                                                shadowOpacity: 0.1,
                                                shadowOffset: { width: 0, height: 2 },
                                                shadowRadius: 1.0,

                                            }}>
                                                <CardItem
                                                    style={{ paddingTop: ScaleHelpers.CalcHeight(14), paddingLeft: ScaleHelpers.CalcWidth(14), paddingBottom: ScaleHelpers.CalcHeight(14) }}
                                                >
                                                    <Left>
                                                        <View style={{
                                                            width: ScaleHelpers.CalcWidth(60),
                                                            height: ScaleHelpers.CalcHeight(60),
                                                            borderRadius: ScaleHelpers.CalcWidth(60) / 2,
                                                            margin: 0,
                                                            padding: 0,
                                                            justifyContent: 'center',
                                                            backgroundColor: '#FF004E',
                                                            elevation: 5,
                                                            shadowColor: "#000",
                                                            shadowOpacity: 0.1,
                                                            shadowOffset: { width: 0, height: ScaleHelpers.CalcHeight(2) },
                                                            shadowRadius: 1.0,
                                                        }}>
                                                            <Image source={bmwerror} style={{ alignSelf: 'center' }} />
                                                        </View>
                                                        <Body style={{ marginLeft: ScaleHelpers.CalcWidth(23) }}>
                                                            <Text style={styles.cardUpperText}>{notif.msg}</Text>
                                                            <Text style={styles.cardLowerText}>{notif.axle}{notif.side}</Text>
                                                        </Body>
                                                    </Left>
                                                </CardItem>
                                            </Card>

                                        </View>
      )
   })

 }
</Swiper>

【问题讨论】:

    标签: react-native swiper


    【解决方案1】:

    来自模块的文档:

    动态内容 如果幻灯片状态或道具发生变化,则不会重新渲染 Swiper。幻灯片必须控制其状况。

    这意味着您必须通过幻灯片而不是通过 Swiper 组件自行管理状态更改。

    编辑:

    因此,您可以创建一个自定义幻灯片模块来处理来自服务器的幻灯片:

    class Slide extends React.Component {
    constructor(props) {
        super(props);
        // your state for the slide component goes here....
    }
    render() {
        const { msg, axle, side } = this.props.notification; // destructure your notification prop being passed from the map method of parent component
        return (
            <View style={[styless.slideContainer]}>
                <Card transparent style={{
                    flex: 0,
                    elevation: 3,
                    shadowColor: "#000",
                    shadowOpacity: 0.1,
                    shadowOffset: { width: 0, height: 2 },
                    shadowRadius: 1.0,
    
                }}>
                    <CardItem
                        style={{ paddingTop: ScaleHelpers.CalcHeight(14), paddingLeft: ScaleHelpers.CalcWidth(14), paddingBottom: ScaleHelpers.CalcHeight(14) }}
                    >
                        <Left>
                            <View style={{
                                width: ScaleHelpers.CalcWidth(60),
                                height: ScaleHelpers.CalcHeight(60),
                                borderRadius: ScaleHelpers.CalcWidth(60) / 2,
                                margin: 0,
                                padding: 0,
                                justifyContent: 'center',
                                backgroundColor: '#FF004E',
                                elevation: 5,
                                shadowColor: "#000",
                                shadowOpacity: 0.1,
                                shadowOffset: { width: 0, height: ScaleHelpers.CalcHeight(2) },
                                shadowRadius: 1.0,
                            }}>
                                <Image source={bmwerror} style={{ alignSelf: 'center' }} />
                            </View>
                            <Body style={{ marginLeft: ScaleHelpers.CalcWidth(23) }}>
                                {/* use your destructure constants where required */}
                                <Text style={styles.cardUpperText}>{msg}</Text> 
                                <Text style={styles.cardLowerText}>{axle}{side}</Text>
                            </Body>
                        </Left>
                    </CardItem>
                </Card>
    
            </View>
        );
    }
    }export default Slide;
    

    我在 cmets 中提到了如何从通知对象中获取所需信息。 现在是发送部分,在您的地图函数中,只需调用您新创建的 Slide 组件并将通知对象作为道具传递,如下所示:

    <Slide notification={notif}/>
    

    注意这里,notification={notif} 中的通知将作为 Slide 组件中的道具名称,您必须从 this.props.notification 对象中提取信息,如前所述。

    【讨论】:

    • 你能给我举个例子吗?我不明白该怎么做
    • 展示你的 Swiper 实现并分享一些与你的幻灯片相关的代码。否则你也可以在他们的文档中找到实现。
    • 我将分享一般结构。
    • 我分享了我的代码,问题是我不知道我会有多少个滑块,这个数字是通过后端的一些计算生成的
    • 这绝对没问题,但我相信你应该有一些组件可以显示来自后端的动态幻灯片。分享该组件的实现,特别是我说的是“//一些视图”部分。
    猜你喜欢
    • 2021-06-25
    • 2019-11-25
    • 2021-09-20
    • 2019-09-23
    • 2020-12-07
    • 2020-11-05
    • 2019-04-11
    • 1970-01-01
    • 2021-02-22
    相关资源
    最近更新 更多