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

How to use this lib as a react component #2

Closed
0xqd opened this issue Jun 8, 2022 · 6 comments
Closed

How to use this lib as a react component #2

0xqd opened this issue Jun 8, 2022 · 6 comments

Comments

@0xqd
Copy link

0xqd commented Jun 8, 2022

Hi, this lib looks fantastic and fast! How do we use this as a react component?
thanks

@laurentpayot
Copy link
Owner

laurentpayot commented Jun 8, 2022

Thanks @rhacker! I don’t know React super well (I use Elm instead), but I created the following examples on CodePen:

Let me know if it works for you.

@SleepyBoy29
Copy link

i think there is some problem in index.html, i tried change import { identiconSvg } from "./minidenticons.min.js" into import { identicon } from "https://unpkg.com/minidenticons@1.0.3/minidenticons.min.js"; and it works, try it out @rhacker

@0xqd 0xqd closed this as completed Jun 24, 2022
@laurentpayot
Copy link
Owner

Yet another way is to extend the React.Component class: https://codepen.io/laurentpayot/pen/gOKbaJV

@danyi1212
Copy link

danyi1212 commented Mar 27, 2023

It's usually better to include SVG inside <img /> tag using Data URI, especially when the SVG provided as string.
See this stack overflow answer for more info - https://stackoverflow.com/a/58456376/8116615

Here is a complete React component to use this concept:

import { identicon } from "minidenticons"
import React, { ImgHTMLAttributes, useMemo } from "react"

interface IdentityIconProps extends Omit<ImgHTMLAttributes<HTMLImageElement>, "src"> {
    username: string
    saturation?: string | number
    lightness?: string | number
}

const IdentityIcon: React.FC<IdentityIconProps> = ({ username, saturation, lightness, ...props }) => {
    const svgText = useMemo(() => identicon(username, saturation, lightness), [username, saturation, lightness])
    return <img src={`data:image/svg+xml;utf8,${encodeURIComponent(svgText)}`} alt={username} {...props} />
}

export default IdentityIcon

Then it can be used like so:

<IdentityIcon username="Hello world" width={300} />

@laurentpayot
Copy link
Owner

Thanks @danyi1212! I’m actually using <img /> data URI with the data:image/svg+xml;utf8, prefix myself in an app. It’s worth mentioning in the docs. I will try to find some time to include your example in the readme.

@laurentpayot
Copy link
Owner

laurentpayot commented Apr 10, 2023

@danyi1212 I simplified your example from TS to JS and put it in the v3 docs with a link to your original comment. I also created one more CodePen for this:

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