Wrap the ImageGallery component with the ErrorBoundary.
// src/components/ImageGallery.js
import ErrorBoundary from './ErrorBoundary';
// Wrap the ImageGallery component with the ErrorBoundary
const ImageGallery = React.memo(() => {
// ... (no changes)
});
const GalleryWithBoundary = () => (
<ErrorBoundary>
<ImageGallery />
</ErrorBoundary>
);
export default GalleryWithBoundary;
Task:
Implement the suggested optimisation's in their own ways.
Measure the performance improvements using browser developer tools.
Explain the reasoning behind their optimisation choices.
Discuss any trade-offs made during the optimisation process.
Consider additional features or improvements they can add to the project.
Encouraging students to experiment with these optimisations will provide them with a deeper understanding of React and how to write more efficient and maintainable code.
Optimization Suggestions:
Lazy Loading with
react-lazyload
:react-lazyload
library for better control over lazy loading.LazyLoad
component that can be wrapped around the images.State Batching:
Memoization:
React.memo
higher-order component to memoize theImageGallery
component and prevent unnecessary renders.CSS-in-JS for Styling:
styled-components
for better styling and component encapsulation.Error Boundary:
ImageGallery
component with theErrorBoundary
.Task:
Encouraging students to experiment with these optimisations will provide them with a deeper understanding of React and how to write more efficient and maintainable code.