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

Add onError to img element #134

Open
cjke opened this issue Jan 20, 2017 · 8 comments
Open

Add onError to img element #134

cjke opened this issue Jan 20, 2017 · 8 comments

Comments

@cjke
Copy link

cjke commented Jan 20, 2017

Gravatar accepts a type of "404" - which, if the email is invalid, will return a 404 status code (instead of say retro or wavatar, etc).

Currently though, there is no way to capture that and action via react-gravatar. It would be nice to be able to pass in a onError prop, and let it be mounted to the img element. This way, a parent component, could catch it, and render a more app relevant default image.

Alternatively, a defaultComponent prop could be passed, that when a 404 is returned, the react-gravatar would render it instead.

Happy to PR if this aligns with the projects goals

@KyleAMathews
Copy link
Owner

How about instead we just support a new default called custom? And when default="custom" and gravatar returns null, the component uses as the image src a prop called defaultSrc. How's that sound?

@cjke
Copy link
Author

cjke commented Jan 21, 2017

If the react-gravatar component returns null when default="404" that would suffice my requirements - even without the defaultSrc or custom pieces.

The issue at the moment is that the 404 status code from gravatar is currently lost, because it is never captured. If that makes sense?

I don't like filing issues with "It doesn't work for my particular use-case" ... but .... in my particular use-case, I want to render a custom avatar component if they don't have a gravatar account. So if their username is "cjke" it would display a "C" in a circle, if their username is "Kyle", it would show a "K" in a circle. So the fallback is a complete component, not just an image.

So yes, if react-component returned a null if there is a 404, then I could check the nullish value and render out a different component instead.

@KyleAMathews
Copy link
Owner

Ah gotcha — yeah that makes sense — a backup default component is way more flexible than a backup src.

Cool, let's just go with your original proposal then. Someone could do what I was proposing with a simple img component and if someone in the future wants to support passing in a backup default src string, I'd accept that too.

@cjke
Copy link
Author

cjke commented Jan 24, 2017

Groovy I will get a PR together shortly.

@cjke
Copy link
Author

cjke commented Jan 25, 2017

Added PR at #135

I've also updated the demo page to demonstrate a simple example of a fallback component.

image

@hongbo-miao
Copy link

The PR is actually very useful.. Reopen?

@zhuber
Copy link

zhuber commented Jul 11, 2018

I need this specific thing, can we please make this happen?

zhuber added a commit to zhuber/react-gravatar that referenced this issue Jul 12, 2018
This which allows users to specify a fallback component, which is great for folks who do not want to fallback to a different image source. For example, users can have a "default profile" component that gets passed in as the `fallback` prop.

# Related feature request:

KyleAMathews#134
@zhuber
Copy link

zhuber commented Jul 12, 2018

I have created a PR that provides this functionality: #146

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants