Image is not displayed for avatar in browser - using Material UI - reactjs

I am newbie and trying to get hands-on working on a project. I have referred to the other questions but they are not similar to this. I am unsure what I am missing here.
I have imported the image in Component-1 and sending it as props to Component-2
here is my code
**Component-1**
import HomeIcon from '#mui/icons-material/Home';
import HeaderOption from './HeaderOption';
import avatar from '../src/images/avatar.jpg';`enter code here`
function Header() {
return (
<div className='right_header'>
<HeaderOption Icon={HomeIcon} title='Home' />
<HeaderOption avatarIcon={avatar} title='Me' />
</div>
);
}
export default Header;
**component-2**
import React from 'react';
import './HeaderOption.css';
import AccountCircleIcon from '#mui/icons-material/AccountCircle';
function HeaderOption({ avatarIcon, Icon, title }) {
return (
<div className='headerOption'>
{Icon && <Icon className='headerOption_icon' />}
{avatarIcon && (
<AccountCircleIcon className='headerOption_icon' src={avatarIcon} />
)}
<h3 className='headerOption_title'>{title}</h3>
</div>
);
}
export default HeaderOption;

I assume what you need is this:
import * as React from 'react';
import Avatar from '#mui/material/Avatar';
import Stack from '#mui/material/Stack';
export default function ImageAvatars() {
return (
<Stack direction="row" spacing={2}>
<Avatar alt="Remy Sharp" src="/static/images/avatar/1.jpg" />
<Avatar alt="Travis Howard" src="/static/images/avatar/2.jpg" />
<Avatar alt="Cindy Baker" src="/static/images/avatar/3.jpg" />
</Stack>
);
}
AccountCircleIcon is an icon and you can't change that. Therefore what you should do is this:
avatarIcon && <Avatar alt="Cindy Baker" className='headerOption_icon' src={avatarIcon} /> )}

Related

Why material-ui components don't display in my App?

So I'm following a react tutorial and in that project material-ui is being used, it's e-commerce, so I have Products.jsx and Product.jsx files.
Product.jsx file:
import React from 'react';
import { Card, CardMedia, CardContent, CardActions, Typography, IconButton} from '#material-ui/core';
import { AddShoppingCart } from '#material-ui/icons';
import MuiThemeProvider from '#material-ui/core/styles/MuiThemeProvider';
const Product = ( {product} ) => {
return (
<MuiThemeProvider>
<Card className="rootie">
<CardMedia className="media" image='' title={product.name} />
<CardContent>
<div className="cardContent">
<Typography variant="h5" gutterBottom>
{product.name}
</Typography>
<Typography variant="h5" gutterBottom>
{product.price}
</Typography>
</div>
<Typography variant="h2" color="textSecondary">{product.description}</Typography>
</CardContent>
<CardActions disableSpacing className="cardActions">
<IconButton aria-label="Add to cart">
<AddShoppingCart/>
</IconButton>
</CardActions>
</Card>
</MuiThemeProvider>
);
}
export default Product;
Products.jsx:
import React from 'react';
import { Grid } from '#material-ui/core';
import Product from '../Product/Product';
import MuiThemeProvider from '#material-ui/core/styles/MuiThemeProvider';
const products = [
{ id: 1, name: 'JBL', description: 'JBL', price: '$100'},
{ id: 2, name: 'AirPods', description: 'AirPods', price: '$100'},
];
const Products = () => {
return (
<MuiThemeProvider>
<Grid container justifyContent="center" spacing={4}>
{products.map((product) =>{
<Grid item key={product.id} xs={12} sm={6} md={4} lg={3}>
<Product product={product}/>
</Grid>
})}
</Grid>
</MuiThemeProvider>
);
}
export default Products;
And my App.js looks like this:
import React from 'react';
import Header from './components/Header/Header';
import Products from './components/Products/Products';
import './App.scss';
const App = () => {
return (
<div>
<Products/>
</div>
)
}
export default App;
So as you see, everything looks okay, but for some reason React doesn't render the components, so the page looks empty. I've searched on the internet and found about 'MuiThemeProvider', but adding it didn't really help either.
You need to create a theme and pass it as prop to ThemeProvider which in turn wraps the entire App during ReactDOM.render stage itself. Something like this:
import * as React from 'react';
import ReactDOM from 'react-dom';
import CssBaseline from '#mui/material/CssBaseline';
import { ThemeProvider } from '#mui/material/styles';
import App from './App';
import theme from './theme';
ReactDOM.render(
<ThemeProvider theme={theme}>
{/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
<CssBaseline />
<App />
</ThemeProvider>,
document.querySelector('#root'),
);
Only then will your Material UI components work as you expect.

InvalidCharacterError: Failed to execute 'createElement' on 'Document':The tag name provided ('/static/media/avatar.6043f57a.png') is not a valid name

Hello Guys I am new to react and trying to clone Netflix through a youtube tutorial. So the issue here is that I am using an Image in a functional component, but it is not rendering properly
The error I am getting is this:
InvalidCharacterError: Failed to execute 'createElement' on 'Document': The tag name provided ('/static/media/avatar.6043f57a.png') is not a valid name.
My Functional Code
import './Header.css';
import SearchIcon from '#material-ui/icons/Search';
import HeaderOption from './HeaderOption';
import HomeIcon from '#material-ui/icons/Home';
import SupervisorAccountIcon from '#material-ui/icons/SupervisorAccount';
import BusinessCenterIcon from '#material-ui/icons/BusinessCenter';
import ChatIcon from '#material-ui/icons/Chat';
import NotificationsIcon from '#material-ui/icons/Notifications';
import TabImg from './avatar.png';
function Header() {
return (
<div className="header">
<div className="header__left">
<img src="https://www.linkpicture.com/q/logo_9.svg" alt="Logo" />
<div className="header__search">
<SearchIcon />
<input type="text"/>
</div>
</div>
<div className="header__right">
<HeaderOption Icon={HomeIcon} title="Home" />
<HeaderOption Icon={SupervisorAccountIcon} title="My Network"/>
<HeaderOption Icon={BusinessCenterIcon} title="Jobs" />
<HeaderOption Icon={ChatIcon} title="Messaging" />
<HeaderOption Icon={NotificationsIcon} title="Notifications" />
<HeaderOption Avatar={TabImg} title="me"/>
</div>
</div>
)
}
export default Header
My HeaderOption Code:
import './HeaderOption.css';
function HeaderOption({Avatar, Icon, title }) {
return (
<div className="headerOption">
{Icon && <Icon className="headerOption__Icon" />}
{Avatar && (<Avatar className="headerOption__Icon" src={Avatar} />)}
<h3 className="headerOption__title">{title}</h3>
</div>
)
}
export default HeaderOption
<Avatar className="headerOption__Icon" src={Avatar} />)
You're doubling up on the Avatar symbol name, trying to use is as both a component name and src prop value. Within the HeaderOption functional component, Avatar only resolves to the prop value so it's like trying to use
</static/media/avatar.6043f57a.png src="/static/media/avatar.6043f57a.png" />
which obviously isn't valid markup.
Try importing the Avatar component using a different name. For example
// for a default export
import AvatarImg from "northern/air/temple"
// or for a named export
import { Avatar as AvatarImg } from "northern/air/temple"
// snip...
{Avatar && (<AvatarImg className="headerOption__Icon" src={Avatar} />)}
<!-- 👆 note the component name -->

Dynamic data are not rendering on the browser

Good day, I'm trying to create a MEARN stack application with react-redux. I am already able to post data to my MongoDB atlas, and I am also able to fetch the data and console.log it. But when I try to render the data to my front end the browser returns an empty page. I would like to know what is causing this issue. This is my first time creating a react application.
import './Experience.css';
import Post from './Posts/Post';
import { Grid, CircularProgress } from '#material-ui/core';
import { useSelector } from 'react-redux';
function Experience() {
const posts = useSelector((state) => state.posts);
console.log(posts); //Logged my data on DevTool -> Console (see image below)
return (
!posts.length ? <CircularProgress /> : (
<Grid container alignItems="stretch" spacing={3}>
Test //view browser
{posts.map((post) => (
<Grid key={post._id} item xs={3} >
<Post post={post} />
</Grid>
))}
</Grid>
)
);
}
export default Experience;
import React from 'react';
import { Card, CardActions, CardContent, CardMedia, IconButton, Typography, CardHeader, Avatar } from '#material-ui/core';
import MoreVertIcon from '#material-ui/icons/MoreVert';
const Post = ({ post }) => {
return (
<Card>
<CardMedia image={post.companyLogo} />
<CardHeader
avatar={
<Avatar aria-label="recipe">
<img href={post.employerImage}/>
</Avatar>
}
action={
<IconButton aria-label="settings">
<MoreVertIcon />
</IconButton>
}
title={post.employerName}
subheader={post.jobTitle}
/>
<CardContent>
<Typography variant="body2" color="textSecondary" component="p">{post.responsibility.map((tag) => `🖱️${tag} `)}</Typography>
</CardContent>
<CardActions>
<Typography variant="body2" color="textSecondary" component="p">{post.employedDate}</Typography>
</CardActions>
</Card>
);
}
export default Post;
Since you are getting response from the db, there must be a rendering error.
From a quick look I see that you have missused the Avatar component.
It should be like: <Avatar alt="recipe" src={post.employerImage} />
or if you want to utilize the children prop, img tag takes src not href:
<Avatar aria-label="recipe">
<img src={post.employerImage}/>
</Avatar>

Placing logo image into navbar

I am getting a broken image when I put my logo inside of the navbar. I have it saved locally inside the public folder.
I have tried passing as a prop, and directly linking to the path, but i cannot seem to get it to work.
import AppBar from '#material-ui/core/AppBar'
import Toolbar from '#material-ui/core/Toolbar'
import Typography from '#material-ui/core/Typography'
const NavBar = () => {
return(
<div>
<AppBar className='nav-bar' position="static" color='white'>
<Toolbar>
<Typography variant="title" >
<img src='../../public/LQ_Logo.png' width={182} height={64} />
</Typography>
</Toolbar>
</AppBar>
</div>
)
};
export default NavBar;
I need the logo to be displayed in the left corner of the navbar
For img within Public folder use relative path to Public.
For public/Vector.png use ./Vector.png.
Moreover, if it's a SVG you can import it as ReactComponent, for example:
import { ReactComponent as CatImg } from "./add-debug.svg";
const NavBar = () => {
return (
<div>
<AppBar className="nav-bar" position="static" color="white">
<Toolbar>
<Typography variant="title">
<CatImg height={100} />
</Typography>
</Toolbar>
<Toolbar>
<Typography variant="title">
<img src="./Vector.png" alt="bug" height={100} />
</Typography>
</Toolbar>
</AppBar>
</div>
);
};
Demo:
Did you check that relative path is correct? Maybe something is missing in "../../public/LQ_Logo.png".
You can try to use this alternative:
import { ReactComponent as Logo } from './logo.svg';
const App = () => (
<div>
{/* Logo is an actual React component */}
<Logo />
</div>
);
Change the png image to svg (is better to escalate width/heigh for web), and then import the image as a component and call the component in your code.

How to add Multiple Buttons in AppBar using iconElementRight

<AppBar
title={<span>Title</span>}
iconRightElement={
<FlatButton key={1} label="About"/>
<FlatButton key={2} label="Home" />
} />
/>
I have tried above code but not working..
Add one parent element
<div>
<FlatButton key={1} label="About"/>
<FlatButton key={2} label="Home" />
<div>
The solution in my opinion would be to wrap all your buttons/icons in one single element and then pass it to your AppBarcomponent via the attribute iconRightElement.
See full example below. Hope this helps:
import Link from 'next/link'
import React, {PropTypes} from 'react'
import ReactDOM from 'react-dom';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import IconButton from 'material-ui/IconButton';
import AppBar from 'material-ui/AppBar';
import NavigationClose from 'material-ui/svg-icons/navigation/close';
import ActionHome from 'material-ui/svg-icons/action/home';
import FlatButton from 'material-ui/FlatButton';
import FontIcon from 'material-ui/FontIcon';
const rightButtons = (
<div className="appBarIcons">
<Link href="/">
<IconButton><ActionHome style={buttonStyle}/></IconButton>
</Link>
<Link href="/Login">
<FlatButton label="Login" style={buttonStyle}/>
</Link>
</div>
);
const buttonStyle = {
color: 'white'
}
class Header extends React.Component {
render(){
return (
<div>
<MuiThemeProvider>
<div>
<AppBar
title="AppTitle"
iconClassNameRight="muidocs-icon-navigation-expand-more"
iconElementRight={rightButtons}
/>
</div>
</MuiThemeProvider>
</div>
)
}
}
export default Header

Resources