Skip to content

Commit a751cec

Browse files
authored
Merge pull request #58 from kopax/patch-1
Update README.md
2 parents 002e811 + 040c547 commit a751cec

File tree

1 file changed

+21
-38
lines changed

1 file changed

+21
-38
lines changed

README.md

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -83,52 +83,35 @@ export default class Screen extends React.Component {
8383

8484
### Dynamic content
8585

86-
The swiper content re-render itself by keep track of `activeIndex` and `index`.
86+
The slide automatically gets `props.activeIndex` and `props.index`.
8787

8888
```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-
11089
import React from 'react';
11190
import { Text, View } from 'react-native';
11291
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 />
12192

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,
12996
}
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.
130112

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**
132115
133116
---
134117

0 commit comments

Comments
 (0)