Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(image): can't change image after set status to 'error' #67

Merged
merged 2 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ const ImageInternal: CompoundedComponent<ImageProps> = ({
// Keep order end

React.useEffect(() => {
if (isError) {
setStatus('normal');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有点不对这个,会把上面的状态冲掉,这个优先级比上面的低,应该放在上面

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,我修改一下

}
if (isCustomPlaceholder) {
setStatus('loading');
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useEffect 依赖里是不是应该加上 isError 和 isCustomPlaceholder

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的逻辑是 src 变化的时候重置状态,如果加上 isError isCustomPlaceholder 的依赖变化就不对了

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里只需要依赖imgSrc吧

Expand Down
15 changes: 15 additions & 0 deletions tests/fallback.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,19 @@ describe('Fallback', () => {

expect(onErrorMock).toHaveBeenCalledTimes(1);
});

it('should change image, not error', () => {
const wrapper = mount(<Image width={200} src="error" fallback='https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png' />);
act(() => {
wrapper.setProps({
src: 'https://gw.alipayobjects.com/mdn/rms_08e378/afts/img/A*NZuwQp_vcIQAAAAAAAAAAABkARQnAQ',
});
jest.runAllTimers();
wrapper.update();
});
expect(wrapper.find('.rc-image-img').prop('src')).toBe(
'https://gw.alipayobjects.com/mdn/rms_08e378/afts/img/A*NZuwQp_vcIQAAAAAAAAAAABkARQnAQ',
);
});

});