Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to extract the the alt value form a jsx image element?

I have this logic where I want to get the alt attribute value of an image element, my first idea way to simply make the value of the key and array that holds a string and image as well ,like this key : [‘some string’, ] but then I realized I could leverage the alt if this can be possible ?

icons: { 
    iconA: <img style={imgStyle} alt='tool for food' src={iconA} />,
    iconB: <img style={imgStyle} alt='tool for drink' src={ iconB} />,
    }
    
    const convertedObject = useMemo(() => {
    return Object.entries(context.icons).map((e) => ({ equipment: e[1].alt  src: e[1] }));}, []);

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Try this:

const convertedObject = useMemo(() => {
    return Object.entries(context.icons).map((e) => ({ 
        equipment: e[1].props.alt,
        src: e[1].props.src,
    }));
}, []);

Maybe you want to add "context.icons" to the useMemo dependecy list.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading