Sorting an array of objects does not update img src
See original GitHub issueDescription
This only occurs in a production build.
When providing an array of objects that include an image source url to a component, you would expect the properties to display per object.
Shuffling the array randomizes everything, but React seems to re-render everything other than the img tag.
Various sort methods to randomize the array have been tried with the same result.
Code example: https://github.com/corymcdaniel/react-sorted-images Live example: https://sorted-images-bug.netlify.com/
Refresh the page a few times and you’ll see that the text changes but the image does not.
I’ve tried this in a CRA project and it doesn’t reproduce the issue, so I’m assuming this has to do with the default Gatsby build. I’ve tried adding keys to the img
tag, putting a hash and key on the img src, shuffling before or after mapping, all with the same result.
Steps to reproduce
- Create an array of objects that include a url to an image.
- Shuffle array
- Map items to a component that displays the image and any text properties
Expected result
A random list that displays an image and accompanying text
Actual result
Images do not update to their associated text. If you view page-source, it looks correct, but the re-render does not update the img src
Environment
System:
OS: macOS 10.15.2
CPU: (8) x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 10.16.0 - ~/.nvm/versions/node/v10.16.0/bin/node
Yarn: 1.17.3 - ~/.nvm/versions/node/v10.16.0/bin/yarn
npm: 6.13.4 - ~/.nvm/versions/node/v10.16.0/bin/npm
Languages:
Python: 2.7.16 - /usr/bin/python
Browsers:
Chrome: 79.0.3945.117
Firefox: 72.0.1
Safari: 13.0.4
npmPackages:
gatsby: ^2.18.12 => 2.18.12
gatsby-image: ^2.2.34 => 2.2.34
gatsby-plugin-manifest: ^2.2.31 => 2.2.31
gatsby-plugin-offline: ^3.0.27 => 3.0.27
gatsby-plugin-react-helmet: ^3.1.16 => 3.1.16
gatsby-plugin-sharp: ^2.3.5 => 2.3.5
gatsby-source-filesystem: ^2.1.40 => 2.1.40
gatsby-transformer-sharp: ^2.3.7 => 2.3.7
npmGlobalPackages:
gatsby-cli: 2.7.46
I’ve been pulling my hair out on this one.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:13
Top GitHub Comments
@corymcdaniel no need to be sorry and no thanks are required, just wanted to present a cleaner solution for your issue and with that not only help you but someone else that comes across this issue.
@corymcdaniel this issue kept bugging me for a bit and in the meantime i put it on the “back burner” and yesterday i decided to revisit it with a fresh pair of eyes. With that in mind it hit me we could do better than this and avoid the “clunkyness” of using the effect and the timeout.
Below are the steps i took to address this:
Re cloned your repo and installed the dependencies
Installed
@loadable/babel-plugin
,@loadable/component
and@loadable/webpack-plugin
. These additions with the code changes i did will “offload” part of the app to the client side, ensuring the conjunction of generating random numbers and setting the images will function better. You can read more about it here, checked the Gatsby documentation that mentions this approach and component and it’s a bit off, as the example could be improved for better understandability.After the installation went through i changed
gatsby-node.js
to the following:What is happening here is nothing more than injecting the
@loadable/webpack-plugin
into the already existing webpack configuration for Gatsby.Grabbed a couple of images i usually use for issues that will involve working with images into the
static
folder more even to expand the set a bit.Created a new component called
LoadableImageList.js
with the following:This is nothing more than your previous implementation of the code used in
pages\index.js
pages\index.js
to the following:yarn build && yarn serve
and opened uphttp://localhost:9000
and i’m presented with the following:It’s still not ideal, but it removes the need of the code/logic that i originally posted.
I don’t know how far along you moved since this, but if you want to test it out, just let me know if this worked for you. Sounds good?