@@ -83,52 +83,35 @@ export default class Screen extends React.Component {
83
83
84
84
### Dynamic content
85
85
86
- The swiper content re-render itself by keep track of ` activeIndex ` and ` index ` .
86
+ The slide automatically gets ` props. activeIndex` and ` props. index` .
87
87
88
88
``` jsx
89
- import React from ' react' ;
90
- import { Text , View } from ' react-native' ;
91
-
92
- export class SomeDynamicSlide extends React .Component {
93
- state = {
94
- someStateItem: ' someValue' ,
95
- };
96
-
97
- render () {
98
- return (
99
- < View style= {{ flex: 1 , alignItems: ' center' , justifyContent: ' center' }}>
100
- < Text onPress= {() => this .setState ({ someStateItem: ' newValue' })}>
101
- {this .state .someStateItem }
102
- < / Text >
103
- < / View>
104
- );
105
- }
106
- }
107
-
108
- ...
109
-
110
89
import React from ' react' ;
111
90
import { Text , View } from ' react-native' ;
112
91
import Swiper from ' react-native-web-swiper' ;
113
- import { SomeDynamicSlide } from ' someDynamicSlideFile' ;
114
-
115
- class SwiperWrapper extends React .Component {
116
- render () {
117
- return (
118
- < Swiper>
119
-
120
- < SomeDynamicSlide / >
121
92
122
- < View style= {{ flex: 1 , alignItems: ' center' , justifyContent: ' center' }}>
123
- < Text > This is static content slide< / Text >
124
- < / View>
125
-
126
- < / Swiper>
127
- );
128
- }
93
+ type Props = {
94
+ index?: number,
95
+ activeIndex?: number,
129
96
}
97
+ export const SomeSlide = (props : Props ) => (
98
+ < View>
99
+ < Text > {props .activeIndex }/ {props .index }< / Text >
100
+ < / View>
101
+ )
102
+
103
+ export default () => (
104
+ < Swiper>
105
+ < SomeSlide / >
106
+ < SomeSlide / >
107
+ < / Swiper>
108
+ )
109
+ ```
110
+
111
+ This is possible because ` Swiper ` used ` cloneElement ` and inject internally the ` activeIndex ` and ` index ` props to each slide.
130
112
131
- ```
113
+ > ` keys ` are automatically set for each child of ` Swiper ` (when injecting props), if ` activeIndex - index ` is ` 0 ` , it will set the key to ` -1 ` .
114
+ > ** This will cause the new active slide to rerender**
132
115
133
116
---
134
117
0 commit comments