score:0
So thanks to Dergash on Github I've discovered that the problem is not that my list is re-rendered.
Because I am creating a new URL every time with URL.createObjectURL inside of my component and because 'backgroundImage' does not do image prefetching whereas src attribute does I am seeing the flicker.
So my mistake was not creating the URL first and then passing it down into the component.
Working example: https://codesandbox.io/s/funny-http-8vrgv
More details: https://github.com/facebook/react/issues/16187
score:2
Array.map()
allows dynamic rendering of elements in React, so that only those who change their props will re-render (as explained in the documentation).
If you'll use this method - rather than create the full array and render it fully - React will only re-render elements that are supposed to be updated:
props.images.map((imageFile, index) => {
let objectURL = URL.createObjectURL(imageFile);
const newStyles = {
left: `${leftPosition}%`,
top: `${topPosition}%`
}
return (
<div className="Preview" style={{...style, ...newStyles}} key={index}></div>
)
});
Note that I've modified the key
property of each element in the array. It should be unique and I didn't know what v4()
is, so I just used the array indices.
React's documentation actually explains really well about these:
Keys help React identify which items have changed, are added, or are removed
score:2
Your answer is with 'key' property of react components. It's built for such cases Assuming this is render part of your component, you should have something like this:
return this.props.images.map(imageFile => (
<div className="Preview" key={imageFile}></div>
))
Assuming imageFile is a unique string. This way react will rerender your virtual dom but it'll recognise that there is no need to dom update for same elements with same key property
Source: stackoverflow.com
Related Query
- How to dynamically render list of components without re-rendering entire list with every new addition?
- How I can render react components without jsx format?
- How to add components to list dynamically in React with no redundant render?
- How to render components dynamically in Gatsby.js and use code-splitting?
- How does a chained export work in react without the components in between actually rendering the component?
- How to dynamically render Route components in React-router-dom and React
- How can I render list items dynamically and recursively from a nested array?
- How to render the leaf component of tree component without re-render entire tree in ReactJS
- How to render a list of components held as a key value
- How to reduce render time while rendering huge list of items in Ionic React
- How can I render an array of components in react without them unmounting?
- How to render only subchild without rendering all high level component
- How to dynamically render react components based on selected dropdown value
- How to go specific route without rendering some components in React?
- How to add React components dynamically without reload?
- How to render components on click plus without changing the 'state'?
- How can I dynamically render components when enter an input
- React-route and server-side rendering - how to render components with data
- How to render components dynamically based on route name with React router
- how to dynamically render reactjs components from json data
- How Can I render a list dynamically using react-calendar?
- How to list items from object array by category and only render components based on which category was last iterated
- How to render <select> tag without rendering the carat and label name
- How to best render react components dynamically from data model
- How to display a list of dynamically loaded components in nextJS
- How to render FluentUI controls in a HTML page similar to rendering React Components in a script tag inside a Div
- How to loop and render elements in React.js without an array of objects to map?
- How to render react components by using map and join?
- Rendering React components with promises inside the render method
- How do I render sibling elements without wrapping them in a parent tag?
More Query from same tag
- How to get CSRF from django in a separate React App
- In React, what is the proper way to await looking up a value in sessionStorage?
- React Js and Chart Js (Line Chart)
- Simulate Width to Test Responsive React Components
- indexing between states in react
- How to make MUI Fade fade in on render?
- React Reducer- take value from state
- Reach Router Cannot read property 'apply' of null
- Facing event depreceated error in Javascript. Can anyone suggest what to do?
- React router children caught in infinite loop
- event object in Javascript
- How to make a transition when using conditional rendering in React
- Nested Row with Table Row with Bootstrap 4 / ReactJS
- Performing jsx transforms on the client
- view page source does not show page in nextjs
- React fetch doesn't display value from nested object
- React.js: Passing props to props?
- handleSubmit and values not recognized in Formik form
- react-scripts : compilation error while importing SVG
- Trying to refetch the data using Apollo query
- avoid duplicating in Sequelize, Nodejs & Reactjs?
- Is there any alternative to use <Link> outside a <Router> with react?
- How to store values from a list into an array of objects in ReactJS
- How do I quickly simply verify react.js loaded?
- SVG in React Component
- Using React, click the first input show the second input, how to auto focus to the second input?
- How to load html file into iframe element in React?
- How to display nested array of objects from REST call in different sections of the page?
- How to return text input into React component
- Material-UI change margin of FormHelperText when FormControl only for Select