Comments (5)
Hi @Hazantip
You can probably create a custom image component that will handle this for you. If I understood correctly, You want to first have a default placeholder (img1) which will either switch out for the actual image (img2a) or the 'broken image' placeholder (img2b) if img2a is unavailable - is this correct?
In this situation I would probably have img1 as the default img.src value, and then make an async request for img2a. When the response comes back successful, I would change the img.src value to the img2a path. If the response is unsuccessful, you have a choice of trying again or updating the value of img.src to that of the img2b path.
as an alternative to making manual requests for images, you could also use a second img tag with display: none
. When the onLoad
function is executed you have a successful image request and you can make this img tag visible, and the original one display: none
. You can listen for onError
to check if the image is broken.
Finally, you can also consider using a background image (CSS) for img1. If your CSS loads first it should be visible while img2a loads over it. You will have to experiment to find what works best for your situation,
Hope that helps!
from react-masonry-component.
I'm also into this. So far I've found that;
- You can convert your img into an external component and use AJAX to test the image and return the template if positive. Didn't test this through, since the async affected other components.
- Call the onload event like:
<img src={image} onClick={(e) => (this._handleClick(e, item)) } onLoad={(e) => (this._onImgLoad(e, item)) } />
You'll probably need to change the app state or call this.forceUpdate() to show the changes on the DOM. - :
<Masonry
className={'ui-grid'}
elementType={'ul'}
options={masonryOptions}
disableImagesLoaded={false}
updateOnEachImageLoad={true}
onImagesLoaded={this._onImgLoad}
>
The problem I currently have with 2. and 3. is that even after a forceUpdate(), Masonry seems to not to update the grid, or updates it in a way that later triggers a rearange on scroll.
from react-masonry-component.
Thanks everyone! I tried to find solution under default callback function
onImagesLoaded={this.handleImagesLoaded}
,
cause inside it we can access to some variable like images
handleImagesLoaded(images) {....}
and this variable give us some info about loaded images, process, and isAnyBroken; i thought that there was some way to with this check each images on is it loaded.
At this moment is solved my problem this way:
<img style={{ 'transition': '1s ease', 'opacity': 0 }} onLoad={(e) => { e.target.style.opacity = 1; }} className="feed-grid-img" src={obj.data.cover.coverImage.url} alt={obj.data.cover.title} />
Default callback function i use just for reveal all grid with checking is loaded all, then show grid with placeholders, and then with img onLoad reveal each picture
handleImagesLoaded(images) { const isAllLoaded = images.images.length === images.progressedCount; is (isAllLoaded) { ...reveal } }
from react-masonry-component.
@Hazantip I tested a similar solution, and one thing I noticed is that sometimes, after a grid refresh, the first element on the grid wasn't triggering the onload event.
Not sure if that was caused by something on my code, but worth double check, just in case.
from react-masonry-component.
I'm going to close this issue. Please feel free to reopen to discuss further if needed.
from react-masonry-component.
Related Issues (20)
- Is there a way to add a class to the items at the bottom of the grid? HOT 2
- Masonry slide effect does not work HOT 2
- Small Bug HOT 2
- Is there any way to animate fading out elements on update?
- RTL Support HOT 1
- Demo website is broken HOT 2
- How to centre the images? HOT 1
- onImagesLoaded is being called twice HOT 1
- Is it possible to show different widths? HOT 1
- A simple CodeSandbox image gallery example
- Update to React 17 HOT 4
- What are the masonry options properties? HOT 2
- Need Help: Not able to add gutter between the cards.
- Demo not working? HOT 3
- Masonery not correctly layout because force to have invisible columns
- how to fix 'Image elements do not have explicit width and height' in pageSpeed
- Support React 18 HOT 2
- Add support for Scss/css module .
- it works only very first time and then it loses position HOT 1
- I seem to have no control over the height of the masonry component HOT 1
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-masonry-component.