React component allowing for the reuse of avatars

As a novice in React, I've encountered a challenge. My goal is to develop a simple reusable component using MUI. Specifically, I aim to create an avatar component that can be easily customized with different images when called upon. I want the flexibility to pass arguments when declaring this component on various pages. Can someone please provide guidance?


        import * as React from 'react';
        import Avatar from '@mui/material/Avatar';
        import Stack from '@mui/material/Stack';
        import frankieAvatar from '/Users/rodriguezmedia/Desktop/react-counter-app/src/images/oink.png';
        import { AvatarGroup } from '@mui/material';

        const designerOne = {
            src: frankieAvatar
        }

        export default function AvatarDesigners() {
            return (
                <Avatar src={designerOne.src}></Avatar>
            );
        }
    

Answer №1

One simple way to achieve this is shown below.

import * as React from 'react';
import Avatar from '@mui/material/Avatar';
import Stack from '@mui/material/Stack';
import { AvatarGroup } from '@mui/material';


export default function AvatarDesigners({src}) {
    return (
        <Avatar src={src}></Avatar>
    );
}

This is how you can utilize the component:

import * as React from 'react';
import frankieAvatar from '/Users/rodriguezmedia/Desktop/react-counter- 
app/src/images/oink.png';
import AvatarDesigners from './AvatarDesigners'

const designerOne = {
    src: frankieAvatar
}

export default function Profile() {
    return (
        <div>
            <AvatarDesigners src={designerOne.src}></Avatar>
        </div>
    );
}

Answer №2

In the event that the avatar image fails to load, the component will revert to an alternative in the specified sequence: the provided children the first letter of the alt text a generic avatar icon

<Avatar
  sx={{ bgcolor: deepOrange[500] }}
  alt="Remy Sharp"
  src="/broken-image.jpg"
>
  B
</Avatar>
<Avatar
  sx={{ bgcolor: deepOrange[500] }}
  alt="Remy Sharp"
  src="/broken-image.jpg"
/>
<Avatar src="/broken-image.jpg" />

Answer №3

To generate image avatars, you can simply pass standard img properties like src or srcSet to the component.

<Avatar alt="John Doe" src="/static/images/avatar/4.jpg" />
<Avatar alt="Jane Smith" src="/static/images/avatar/5.jpg" />
<Avatar alt="Michael Johnson" src="/static/images/avatar/6.jpg" />

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

List of different components in VueJS

After numerous failed attempts to find the specific solution I need, it seems that my search has been in vain. Nevertheless, here is the query: Imagine I have an array of objects containing a title field and an input type field, among other parameters. Wh ...

The created function in VueJS may sometimes result in an undefined outcome

I recently started a project with VueJs where I encountered an issue while making a GET request using the Axios library. The data was returned as expected, but I faced difficulties calling the loadUsers function inside mounted. Here is my code snippet: ex ...

The instance of my ObjectType is coming back as an empty entity

Having trouble making relationships between two object types in my code. One of them is working fine, but the other one returns an empty object and I can't seem to find the issue. The first one works as expected and logs the rank type without any pro ...

MUI Autocomplete refuses to accept a value

In my Autocomplete feature, I have a list of all users available. When a user clicks on a button from another site, they are redirected to this site and the user's ID is fetched. I want the user with the corresponding ID to be automatically selected, ...

Transferring information to a controller using ajax in ASP.NET Core

I am encountering an issue with sending data to the controller through ajax. The value goes as "null". Can someone please assist me with this? Here are my HTML codes: <div class="modal fade" id="sagTikMenuKategoriGuncelleModal" data ...

The Bootstrap alert refuses to close when the close button is clicked

I'm attempting to utilize a Bootstrap alert for displaying a warning. The alert automatically fades and dismisses after a period of time, but I want to provide the user with the option to manually close it. I've included jQuery and js/bootstrap.m ...

Image expansion

I have a container element div that holds various content of unknown length. How can I add a background image that extends the entire length of the container since background-images do not stretch? I attempted to use a separate div with an img tag inside. ...

Animation in Expansion panel not toggling properly

I have encountered a problem while trying to incorporate a Material UI Switch in an MUI Expansion Panel. The Switch's state is managed within an array of objects and updated through a handler function. However, when I click the switch, the state chang ...

Is JQuery function running on its own or triggered by an event without manual input?

When visitors come to my website, I want them to see two options: one for registration and the other for logging in. To achieve this, I've created two buttons and a function that makes the form/input fields fade in when clicked. However, the issue is ...

Are you transitioning from traditional scroll pagination to using ajax?

Is it possible to replace scroll pagination with ajax? I'm looking for an alternative to the large scroll pagination query and wondering if ajax could be used instead. Here is the current code snippet: feeds.scrollFeedPagination({ 'contentPage ...

How do I start using Google Analytics: application.js, ga.js, or a beginner’s guide?

Hello there! I was wondering if anyone could provide a comprehensive guide or step-by-step tutorial on setting up Google Analytics with some examples. The information I found so far only covers signing up and obtaining a tracking code, but it doesn't ...

Using Typescript and react-redux with a Stateful Component: The parameter type 'typeof MyClass' does not match the expected component type

I've been trying to dive into the world of Typescript, React, and Redux. However, I've hit a roadblock at the moment. This is the error I encountered: ./src/containers/Hello.tsx [tsl] ERROR in /home/marc/Development/TypeScript-React-Starte ...

What is the best way to have the sidebar of a webpage slide out over the main body content that is being displayed?

Issue Summary I am experiencing an issue with the positioning of my sidebar, which is initially located 66% offscreen using -translate-x-2/3. The sidebar is meant to be pulled into view onmouseover, however, the main body content remains stuck in place. I ...

Comparing React with SignalR: A Clash of Real

Currently, I specialize in developing web applications with ASP MVC and am considering incorporating ReactJS into my projects. While I have come across examples of React being integrated into ASP.NET MVC projects, I'm not entirely convinced of its sup ...

Craft a brief description for every individual component discovered

Is there a way to identify and shorten all elements containing a <tspan> tag to just ten characters each? For example: <tspan>Bla bla bla bla</tspan> <tspan>Bla bla bla bla</tspan> <tspan>Bla bla bla bla</tspan> ...

Tips for getting my scripts to function properly with my components in next.js?

I am facing an issue with my script while using React and Next.js in my pages/_app.js file. The code snippet is as follows: import axios from "axios"; import { $ } from "jquery"; import App from "next/app"; import Router from ...

Creating a sticky positioning effect in CSS

Can someone assist me in implementing the position: sticky as demonstrated below? Currently, the upcoming date overlaps the current date. This causes the opacity and shadow of the date to reach 100%, which can create a cluttered appearance if there are m ...

What's the best way to pair a number with a neighboring letter?

const userInput = "2a smith road"; const secondInput = "333 flathead lake road, apartment 3b" const formattedAddress = userInput.replace(/(^\w{1})|(\s+\w{1})/g, letter => letter.toUpperCase()); The final result will ...

Unveiling the Solution: Deactivating CSS Style Hints in VS Code!

click here to view the image Can anyone guide me on how to turn off the style hint feature in VS Code while working with CSS? ...

Error message "Truffle 'Migrations' - cb is not a valid function"

I created a straightforward smart contract using Solidity 0.6.6 and now I'm attempting to deploy it on the BSC Testnet. Here's what my truffle-config.js file looks like (privateKeys is an array with one entry ['0x + privatekey']): netw ...