Skip to content

Commit 5d5e4fb

Browse files
author
Benoît Delmaire
authored
Merge pull request meliorence#650 from brianshano/patch-1
doc(ParallaxImage): Update Hooks example to a more standalone component
2 parents 96c1723 + fdeb56a commit 5d5e4fb

File tree

1 file changed

+85
-45
lines changed

1 file changed

+85
-45
lines changed

doc/PARALLAX_IMAGE.md

Lines changed: 85 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -84,55 +84,94 @@ const styles = StyleSheet.create({
8484
## Example to use with React Hooks
8585

8686
```javascript
87-
import React, { useRef } from 'react'
88-
import Carousel, { ParallaxImage } from 'react-native-snap-carousel';
89-
import { View, Dimensions, StyleSheet } from 'react-native';
87+
import React, {useRef, useState, useEffect} from 'react';
88+
import Carousel, {ParallaxImage} from 'react-native-snap-carousel';
89+
import {
90+
View,
91+
Text,
92+
Dimensions,
93+
StyleSheet,
94+
TouchableOpacity,
95+
Platform,
96+
} from 'react-native';
97+
98+
const ENTRIES1 = [
99+
{
100+
title: 'Beautiful and dramatic Antelope Canyon',
101+
subtitle: 'Lorem ipsum dolor sit amet et nuncat mergitur',
102+
illustration: 'https://i.imgur.com/UYiroysl.jpg',
103+
},
104+
{
105+
title: 'Earlier this morning, NYC',
106+
subtitle: 'Lorem ipsum dolor sit amet',
107+
illustration: 'https://i.imgur.com/UPrs1EWl.jpg',
108+
},
109+
{
110+
title: 'White Pocket Sunset',
111+
subtitle: 'Lorem ipsum dolor sit amet et nuncat ',
112+
illustration: 'https://i.imgur.com/MABUbpDl.jpg',
113+
},
114+
{
115+
title: 'Acrocorinth, Greece',
116+
subtitle: 'Lorem ipsum dolor sit amet et nuncat mergitur',
117+
illustration: 'https://i.imgur.com/KZsmUi2l.jpg',
118+
},
119+
{
120+
title: 'The lone tree, majestic landscape of New Zealand',
121+
subtitle: 'Lorem ipsum dolor sit amet',
122+
illustration: 'https://i.imgur.com/2nCt3Sbl.jpg',
123+
},
124+
];
125+
const {width: screenWidth} = Dimensions.get('window');
90126

91-
const { width: screenWidth } = Dimensions.get('window')
127+
const MyCarousel = props => {
128+
const [entries, setEntries] = useState([]);
129+
const carouselRef = useRef(null);
92130

93-
const MyCarousel = (props) => {
94-
const carouselRef = useRef(null)
131+
const goForward = () => {
132+
carouselRef.current.snapToNext();
133+
};
95134

96-
const goForward = () => {
97-
carouselRef.current.snapToNext()
98-
}
99-
100-
const _renderItem = ({item, index}, parallaxProps) => {
101-
return (
102-
<View style={styles.item}>
103-
<ParallaxImage
104-
source={{ uri: item.thumbnail }}
105-
containerStyle={styles.imageContainer}
106-
style={styles.image}
107-
parallaxFactor={0.4}
108-
{...parallaxProps}
109-
/>
110-
<Text style={styles.title} numberOfLines={2}>
111-
{ item.title }
112-
</Text>
113-
</View>
114-
);
115-
}
135+
useEffect(() => {
136+
setEntries(ENTRIES1);
137+
}, []);
116138

139+
const renderItem = ({item, index}, parallaxProps) => {
117140
return (
118-
<View style={styles.container}>
119-
<TouchableOpacity onPress={goForward}>
120-
<Text>go to next slide</Text>
121-
</TouchableOpacity>
122-
<Carousel
123-
ref={carouselRef}
124-
sliderWidth={screenWidth}
125-
sliderHeight={screenWidth}
126-
itemWidth={screenWidth - 60}
127-
data={this.state.entries}
128-
renderItem={this._renderItem}
129-
hasParallaxImages={true}
130-
/>
131-
</View>
141+
<View style={styles.item}>
142+
<ParallaxImage
143+
source={{uri: item.illustration}}
144+
containerStyle={styles.imageContainer}
145+
style={styles.image}
146+
parallaxFactor={0.4}
147+
{...parallaxProps}
148+
/>
149+
<Text style={styles.title} numberOfLines={2}>
150+
{item.title}
151+
</Text>
152+
</View>
132153
);
133-
}
134-
135-
export default MyCarousel
154+
};
155+
156+
return (
157+
<View style={styles.container}>
158+
<TouchableOpacity onPress={goForward}>
159+
<Text>go to next slide</Text>
160+
</TouchableOpacity>
161+
<Carousel
162+
ref={carouselRef}
163+
sliderWidth={screenWidth}
164+
sliderHeight={screenWidth}
165+
itemWidth={screenWidth - 60}
166+
data={entries}
167+
renderItem={renderItem}
168+
hasParallaxImages={true}
169+
/>
170+
</View>
171+
);
172+
};
173+
174+
export default MyCarousel;
136175

137176
const styles = StyleSheet.create({
138177
container: {
@@ -144,13 +183,14 @@ const styles = StyleSheet.create({
144183
},
145184
imageContainer: {
146185
flex: 1,
147-
marginBottom: Platform.select({ ios: 0, android: 1 }), // Prevent a random Android rendering issue
186+
marginBottom: Platform.select({ios: 0, android: 1}), // Prevent a random Android rendering issue
148187
backgroundColor: 'white',
149188
borderRadius: 8,
150189
},
151190
image: {
152191
...StyleSheet.absoluteFillObject,
153192
resizeMode: 'cover',
154193
},
155-
})
194+
});
195+
156196
```

0 commit comments

Comments
 (0)