Comments (2)
@ajonno Hi! You're going to need the prop onPageSelected
for that. The following code should get you started ;)
const CAPTIONS = ['Caption 1', 'Caption 2', 'Caption 3'];
render () {
const { caption } = this.state;
<View>
<Gallery onPageSelected={(index) => { this.setState({ caption: CAPTIONS[index] }); }} />
<View>
<Text>{ caption }</Text>
</View>
</View>
}
from react-native-image-gallery.
Hi,
here's how I do it
When the user taps the thumbnail, my router (react-navigation or NavigationExperimental depending on the project) opens a modal in which I'm rendering a view I'm calling ImageGallery
.
In this view, I render react-native-gallery
and any view I want on top of it, like the captions or the slide counter.
Here's a dumbed-down example
import React, { Component, PropTypes } from 'react';
import { View, Text } from 'react-native';
import Gallery from 'react-native-image-gallery';
import styles from 'app/src/styles/views/ImageGallery.style';
export default class ImageGallery extends Component {
static propTypes () {
return {
images: PropTypes.PropTypes.arrayOf(
PropTypes.shape({
url: PropTypes.string,
thumbnail: PropTypes.string,
imageId: PropTypes.string,
caption: PropTypes.string
})
).isRequired,
index: PropTypes.number
};
};
constructor (props) {
super(props);
const { index, images } = props;
this.onChangeImage = this.onChangeImage.bind(this);
this.state = {
status: 1,
index: index || 0,
imagesCount: images.length
};
}
_getImagesUrls (images) {
let urls = [];
images.forEach((image) => {
urls.push({
source: { uri: image.url }
});
});
return urls;
}
onChangeImage (index) {
this.setState({ index });
}
get captions () {
const { index } = this.state;
const { images } = this.props;
const captionField = images[index].caption ? (
<Text style={styles.caption} numberOfLines={2}>{images[index].caption}</Text>
) : false;
if (!captionField) {
return false;
}
return (
<View style={styles.captionContainer}>
{ captionField }
<View style={styles.captionLine} />
</View>
);
}
get counter () {
const { index, imagesCount } = this.state;
return (
<View style={styles.counterContainer}>
<Text style={styles.counterCurrent}>{index + 1}</Text>
<Text style={styles.counterTotal}>| {imagesCount}</Text>
</View>
);
}
get gallery () {
const { images, index } = this.props;
return (
<Gallery
style={styles.galleryContainer}
initialPage={index || 0}
onPageSelected={this.onChangeImage}
images={this._getImagesUrls(images)} />
);
}
render () {
return (
<View style={styles.container}>
{ this.gallery }
{ this.captions }
{ this.counter }
</View>
);
}
}
from react-native-image-gallery.
Related Issues (20)
- Error decoding image data <NSData 0x6000030192f0; 82644 bytes>
- It is possible to save the image on my iPhone/Android ? HOT 1
- Laggy when using setParams react navigation on page selected HOT 1
- how to stop photo from swiping vertically when scrolling horizontally
- Warning: componentWillMount/ componentWillReceiveProps deprecated HOT 1
- Example using image in storage
- Feature Request
- infinite scrolling feature
- This library is not MAINTAINED! HOT 7
- keyExtractor HOT 1
- detect when swiped at the last image
- Animated 'useNativeDriver' was not specified HOT 6
- Exit button is not defined by default? HOT 1
- page Scroll not working
- How can i get zoom scale
- zoom to area??
- initialPage doesn't work above index value of 9
- The Gallery seems to break after removing the last image HOT 1
- How to enlarge a picture without shrinking it?Could anyone do me a favor?
- Android header space
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-image-gallery.