Skip to content

Commit

Permalink
[@mantine/core] Image: Fix incorrect behavior when src changes to `…
Browse files Browse the repository at this point in the history
…null` after initial render (#3098)

If you render an Image component with a valid src, then update the src prop to
null, the placeholder does not render. With this fix, the placeholder will be
properly rendered when the component updates.
  • Loading branch information
sctape committed Dec 4, 2022
1 parent 013ed74 commit c07014a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/mantine-core/src/Image/Image.test.tsx
Expand Up @@ -45,6 +45,13 @@ describe('@mantine/core/Image', () => {
expect(withoutPlaceholder.querySelectorAll('.mantine-Image-placeholder')).toHaveLength(0);
});

it('renders a placeholder after having src updated to null', () => {
const { rerender, container } = render(<Image src="test-src" withPlaceholder />);
expect(container.querySelectorAll('.mantine-Image-placeholder')).toHaveLength(0);
rerender(<Image src={null} withPlaceholder />);
expect(container.querySelectorAll('.mantine-Image-placeholder')).toHaveLength(1);
});

it('renders given caption', () => {
const { container: withoutCaption } = render(<Image src="test" />);
const { container: withCaption } = render(<Image src="test" caption="test-caption" />);
Expand Down
2 changes: 1 addition & 1 deletion src/mantine-core/src/Image/Image.tsx
Expand Up @@ -82,7 +82,7 @@ export const Image = forwardRef<HTMLDivElement, ImageProps>((props: ImageProps,
const isPlaceholder = withPlaceholder && error;

useDidUpdate(() => {
setError(false);
setError(!src);
}, [src]);

return (
Expand Down

0 comments on commit c07014a

Please sign in to comment.