Async image gallery in React
I am creating an image gallery in react. I am loading all the thumbnails of the images on one page, rendering the array of images stored in the initial state with map().
What I would like to do is to load the image thumbnails in one by one (once the first loaded fade it in) and then the same with the second etc. like a domino effect. What is the best way to get around the problem?
I have tried to map the array and set an onload on the img tag but that just fades all the images in at once, once all are loaded.
reactjs image image-gallery
add a comment |
I am creating an image gallery in react. I am loading all the thumbnails of the images on one page, rendering the array of images stored in the initial state with map().
What I would like to do is to load the image thumbnails in one by one (once the first loaded fade it in) and then the same with the second etc. like a domino effect. What is the best way to get around the problem?
I have tried to map the array and set an onload on the img tag but that just fades all the images in at once, once all are loaded.
reactjs image image-gallery
add a comment |
I am creating an image gallery in react. I am loading all the thumbnails of the images on one page, rendering the array of images stored in the initial state with map().
What I would like to do is to load the image thumbnails in one by one (once the first loaded fade it in) and then the same with the second etc. like a domino effect. What is the best way to get around the problem?
I have tried to map the array and set an onload on the img tag but that just fades all the images in at once, once all are loaded.
reactjs image image-gallery
I am creating an image gallery in react. I am loading all the thumbnails of the images on one page, rendering the array of images stored in the initial state with map().
What I would like to do is to load the image thumbnails in one by one (once the first loaded fade it in) and then the same with the second etc. like a domino effect. What is the best way to get around the problem?
I have tried to map the array and set an onload on the img tag but that just fades all the images in at once, once all are loaded.
reactjs image image-gallery
reactjs image image-gallery
asked Nov 13 '18 at 1:35
Gabriella Csernus
5210
5210
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
If you want to fadein the image as soon as you load the image, you wont get the domino effect you want because you will load the images at different times. For example, maybe image 4 fadesin first, then image 6, then image 1, or maybe all the images load at once and fadein at the same time.
I recommend once all the images are loaded, you fadein image #1, delay 1 second, fadein image #2, delay 1 second, fadein image #3, to get your Domino effect.
Can I load first in and then fade it in, and then start loading the next? If yes how? At the moment my images are in this.state.ImageUrls and I am mapping through them like so:ImageUrls.map((ImageUrl, index)=> { return (<div ><img key={index} id="thumbnail" onLoad={this.imageLoaded} src={ImageUrl} className={this.state.galleryContent} onClick={(e) => this.openModal(index, ImageUrl, e)}/></div>)})}
– Gabriella Csernus
Nov 13 '18 at 12:07
Change to className=${this.state.galleryContent} ${this.state.imageFadein[index] ? 'fadein' : ''}. Then in your imageLoaded() function, detect when the last of all images are loaded, then perform your domino effect to fadein each subsequent image every second via setTimeout(function() { this.setState({ this.state.imageFadein[0]: true }); setTimeout(function() { this.setState({ this.state.imageFadein[1]: true }); }, 1000); }, 1000);
– Shawn Andrews
Nov 13 '18 at 20:06
your this.state.galleryContent should have opacity: 0, and the fadein class is just a simply opacity change: .fadein { animation: fadein 2s; } @keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
– Shawn Andrews
Nov 13 '18 at 20:11
Do you mean this.setState({ imageFadein[0]: true }); and not this.setState({ this.state.imageFadein[0]: true }); and am I thinking wrong but do you mean creating a new state called imageFadein and mutating its state, not with with my existing ImageUrls where I have my images?
– Gabriella Csernus
Nov 13 '18 at 21:55
I have created the new state. Which stores the fadein true or false, which will set the classname, but I get an error in the imageLoaded functionthis.setState({ imageFadein[0]: true });hmmm
– Gabriella Csernus
Nov 13 '18 at 22:03
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272513%2fasync-image-gallery-in-react%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to fadein the image as soon as you load the image, you wont get the domino effect you want because you will load the images at different times. For example, maybe image 4 fadesin first, then image 6, then image 1, or maybe all the images load at once and fadein at the same time.
I recommend once all the images are loaded, you fadein image #1, delay 1 second, fadein image #2, delay 1 second, fadein image #3, to get your Domino effect.
Can I load first in and then fade it in, and then start loading the next? If yes how? At the moment my images are in this.state.ImageUrls and I am mapping through them like so:ImageUrls.map((ImageUrl, index)=> { return (<div ><img key={index} id="thumbnail" onLoad={this.imageLoaded} src={ImageUrl} className={this.state.galleryContent} onClick={(e) => this.openModal(index, ImageUrl, e)}/></div>)})}
– Gabriella Csernus
Nov 13 '18 at 12:07
Change to className=${this.state.galleryContent} ${this.state.imageFadein[index] ? 'fadein' : ''}. Then in your imageLoaded() function, detect when the last of all images are loaded, then perform your domino effect to fadein each subsequent image every second via setTimeout(function() { this.setState({ this.state.imageFadein[0]: true }); setTimeout(function() { this.setState({ this.state.imageFadein[1]: true }); }, 1000); }, 1000);
– Shawn Andrews
Nov 13 '18 at 20:06
your this.state.galleryContent should have opacity: 0, and the fadein class is just a simply opacity change: .fadein { animation: fadein 2s; } @keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
– Shawn Andrews
Nov 13 '18 at 20:11
Do you mean this.setState({ imageFadein[0]: true }); and not this.setState({ this.state.imageFadein[0]: true }); and am I thinking wrong but do you mean creating a new state called imageFadein and mutating its state, not with with my existing ImageUrls where I have my images?
– Gabriella Csernus
Nov 13 '18 at 21:55
I have created the new state. Which stores the fadein true or false, which will set the classname, but I get an error in the imageLoaded functionthis.setState({ imageFadein[0]: true });hmmm
– Gabriella Csernus
Nov 13 '18 at 22:03
|
show 1 more comment
If you want to fadein the image as soon as you load the image, you wont get the domino effect you want because you will load the images at different times. For example, maybe image 4 fadesin first, then image 6, then image 1, or maybe all the images load at once and fadein at the same time.
I recommend once all the images are loaded, you fadein image #1, delay 1 second, fadein image #2, delay 1 second, fadein image #3, to get your Domino effect.
Can I load first in and then fade it in, and then start loading the next? If yes how? At the moment my images are in this.state.ImageUrls and I am mapping through them like so:ImageUrls.map((ImageUrl, index)=> { return (<div ><img key={index} id="thumbnail" onLoad={this.imageLoaded} src={ImageUrl} className={this.state.galleryContent} onClick={(e) => this.openModal(index, ImageUrl, e)}/></div>)})}
– Gabriella Csernus
Nov 13 '18 at 12:07
Change to className=${this.state.galleryContent} ${this.state.imageFadein[index] ? 'fadein' : ''}. Then in your imageLoaded() function, detect when the last of all images are loaded, then perform your domino effect to fadein each subsequent image every second via setTimeout(function() { this.setState({ this.state.imageFadein[0]: true }); setTimeout(function() { this.setState({ this.state.imageFadein[1]: true }); }, 1000); }, 1000);
– Shawn Andrews
Nov 13 '18 at 20:06
your this.state.galleryContent should have opacity: 0, and the fadein class is just a simply opacity change: .fadein { animation: fadein 2s; } @keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
– Shawn Andrews
Nov 13 '18 at 20:11
Do you mean this.setState({ imageFadein[0]: true }); and not this.setState({ this.state.imageFadein[0]: true }); and am I thinking wrong but do you mean creating a new state called imageFadein and mutating its state, not with with my existing ImageUrls where I have my images?
– Gabriella Csernus
Nov 13 '18 at 21:55
I have created the new state. Which stores the fadein true or false, which will set the classname, but I get an error in the imageLoaded functionthis.setState({ imageFadein[0]: true });hmmm
– Gabriella Csernus
Nov 13 '18 at 22:03
|
show 1 more comment
If you want to fadein the image as soon as you load the image, you wont get the domino effect you want because you will load the images at different times. For example, maybe image 4 fadesin first, then image 6, then image 1, or maybe all the images load at once and fadein at the same time.
I recommend once all the images are loaded, you fadein image #1, delay 1 second, fadein image #2, delay 1 second, fadein image #3, to get your Domino effect.
If you want to fadein the image as soon as you load the image, you wont get the domino effect you want because you will load the images at different times. For example, maybe image 4 fadesin first, then image 6, then image 1, or maybe all the images load at once and fadein at the same time.
I recommend once all the images are loaded, you fadein image #1, delay 1 second, fadein image #2, delay 1 second, fadein image #3, to get your Domino effect.
answered Nov 13 '18 at 6:50
Shawn Andrews
945616
945616
Can I load first in and then fade it in, and then start loading the next? If yes how? At the moment my images are in this.state.ImageUrls and I am mapping through them like so:ImageUrls.map((ImageUrl, index)=> { return (<div ><img key={index} id="thumbnail" onLoad={this.imageLoaded} src={ImageUrl} className={this.state.galleryContent} onClick={(e) => this.openModal(index, ImageUrl, e)}/></div>)})}
– Gabriella Csernus
Nov 13 '18 at 12:07
Change to className=${this.state.galleryContent} ${this.state.imageFadein[index] ? 'fadein' : ''}. Then in your imageLoaded() function, detect when the last of all images are loaded, then perform your domino effect to fadein each subsequent image every second via setTimeout(function() { this.setState({ this.state.imageFadein[0]: true }); setTimeout(function() { this.setState({ this.state.imageFadein[1]: true }); }, 1000); }, 1000);
– Shawn Andrews
Nov 13 '18 at 20:06
your this.state.galleryContent should have opacity: 0, and the fadein class is just a simply opacity change: .fadein { animation: fadein 2s; } @keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
– Shawn Andrews
Nov 13 '18 at 20:11
Do you mean this.setState({ imageFadein[0]: true }); and not this.setState({ this.state.imageFadein[0]: true }); and am I thinking wrong but do you mean creating a new state called imageFadein and mutating its state, not with with my existing ImageUrls where I have my images?
– Gabriella Csernus
Nov 13 '18 at 21:55
I have created the new state. Which stores the fadein true or false, which will set the classname, but I get an error in the imageLoaded functionthis.setState({ imageFadein[0]: true });hmmm
– Gabriella Csernus
Nov 13 '18 at 22:03
|
show 1 more comment
Can I load first in and then fade it in, and then start loading the next? If yes how? At the moment my images are in this.state.ImageUrls and I am mapping through them like so:ImageUrls.map((ImageUrl, index)=> { return (<div ><img key={index} id="thumbnail" onLoad={this.imageLoaded} src={ImageUrl} className={this.state.galleryContent} onClick={(e) => this.openModal(index, ImageUrl, e)}/></div>)})}
– Gabriella Csernus
Nov 13 '18 at 12:07
Change to className=${this.state.galleryContent} ${this.state.imageFadein[index] ? 'fadein' : ''}. Then in your imageLoaded() function, detect when the last of all images are loaded, then perform your domino effect to fadein each subsequent image every second via setTimeout(function() { this.setState({ this.state.imageFadein[0]: true }); setTimeout(function() { this.setState({ this.state.imageFadein[1]: true }); }, 1000); }, 1000);
– Shawn Andrews
Nov 13 '18 at 20:06
your this.state.galleryContent should have opacity: 0, and the fadein class is just a simply opacity change: .fadein { animation: fadein 2s; } @keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
– Shawn Andrews
Nov 13 '18 at 20:11
Do you mean this.setState({ imageFadein[0]: true }); and not this.setState({ this.state.imageFadein[0]: true }); and am I thinking wrong but do you mean creating a new state called imageFadein and mutating its state, not with with my existing ImageUrls where I have my images?
– Gabriella Csernus
Nov 13 '18 at 21:55
I have created the new state. Which stores the fadein true or false, which will set the classname, but I get an error in the imageLoaded functionthis.setState({ imageFadein[0]: true });hmmm
– Gabriella Csernus
Nov 13 '18 at 22:03
Can I load first in and then fade it in, and then start loading the next? If yes how? At the moment my images are in this.state.ImageUrls and I am mapping through them like so:
ImageUrls.map((ImageUrl, index)=> { return (<div ><img key={index} id="thumbnail" onLoad={this.imageLoaded} src={ImageUrl} className={this.state.galleryContent} onClick={(e) => this.openModal(index, ImageUrl, e)}/></div>)})}– Gabriella Csernus
Nov 13 '18 at 12:07
Can I load first in and then fade it in, and then start loading the next? If yes how? At the moment my images are in this.state.ImageUrls and I am mapping through them like so:
ImageUrls.map((ImageUrl, index)=> { return (<div ><img key={index} id="thumbnail" onLoad={this.imageLoaded} src={ImageUrl} className={this.state.galleryContent} onClick={(e) => this.openModal(index, ImageUrl, e)}/></div>)})}– Gabriella Csernus
Nov 13 '18 at 12:07
Change to className=
${this.state.galleryContent} ${this.state.imageFadein[index] ? 'fadein' : ''}. Then in your imageLoaded() function, detect when the last of all images are loaded, then perform your domino effect to fadein each subsequent image every second via setTimeout(function() { this.setState({ this.state.imageFadein[0]: true }); setTimeout(function() { this.setState({ this.state.imageFadein[1]: true }); }, 1000); }, 1000);– Shawn Andrews
Nov 13 '18 at 20:06
Change to className=
${this.state.galleryContent} ${this.state.imageFadein[index] ? 'fadein' : ''}. Then in your imageLoaded() function, detect when the last of all images are loaded, then perform your domino effect to fadein each subsequent image every second via setTimeout(function() { this.setState({ this.state.imageFadein[0]: true }); setTimeout(function() { this.setState({ this.state.imageFadein[1]: true }); }, 1000); }, 1000);– Shawn Andrews
Nov 13 '18 at 20:06
your this.state.galleryContent should have opacity: 0, and the fadein class is just a simply opacity change: .fadein { animation: fadein 2s; } @keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
– Shawn Andrews
Nov 13 '18 at 20:11
your this.state.galleryContent should have opacity: 0, and the fadein class is just a simply opacity change: .fadein { animation: fadein 2s; } @keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
– Shawn Andrews
Nov 13 '18 at 20:11
Do you mean this.setState({ imageFadein[0]: true }); and not this.setState({ this.state.imageFadein[0]: true }); and am I thinking wrong but do you mean creating a new state called imageFadein and mutating its state, not with with my existing ImageUrls where I have my images?
– Gabriella Csernus
Nov 13 '18 at 21:55
Do you mean this.setState({ imageFadein[0]: true }); and not this.setState({ this.state.imageFadein[0]: true }); and am I thinking wrong but do you mean creating a new state called imageFadein and mutating its state, not with with my existing ImageUrls where I have my images?
– Gabriella Csernus
Nov 13 '18 at 21:55
I have created the new state. Which stores the fadein true or false, which will set the classname, but I get an error in the imageLoaded function
this.setState({ imageFadein[0]: true }); hmmm– Gabriella Csernus
Nov 13 '18 at 22:03
I have created the new state. Which stores the fadein true or false, which will set the classname, but I get an error in the imageLoaded function
this.setState({ imageFadein[0]: true }); hmmm– Gabriella Csernus
Nov 13 '18 at 22:03
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272513%2fasync-image-gallery-in-react%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown