Is it possible to apply withStyles to the output of a function?

Currently, I am attempting to add extra styling to the output of a function that returns one of two components based on specific props. Here is an example:

import React from 'react'
import { withStyles } from '@material-ui/core/styles'
import Avatar from '@material-ui/core/Avatar'
import AccountCircle from '@material-ui/icons/AccountCircle'

const DefaultImage = withStyles({
  root: {
    backgroundColor: 'rgba(255, 255, 255, 0.5)',
    width: '40px',
    height: '40px',
    borderRadius: '50%'
  }
})(AccountCircle)

export default function UserAvatar(props) {
  return props.userImage ? <Avatar src={props.userImage} /> : <DefaultImage />
}

Despite this setup, when trying to style the result using withStyles, the additional styles do not display. Is my approach correct in this situation?

import React from 'react'
import UserAvatar from './avatar.js'
import { withStyles } from '@material-ui/core/styles'

const UserAvatarHeader = withStyles({
  root: {
    margin: '2vh',
    float: 'right'
  }
})(UserAvatar)

export default function PageHeader(props) {
  return (
...
      <UserAvatarHeader userImage={props.userImage} />
...
  )
}

Answer №1

Check out this alternative approach

import React from 'react'
import { withStyles } from '@material-ui/core/styles'
import Avatar from '@material-ui/core/Avatar'
import AccountCircle from '@material-ui/icons/AccountCircle'

const styles = {
  default: {
    backgroundColor: 'rgba(255, 255, 255, 0.5)',
    width: '40px',
    height: '40px',
    borderRadius: '50%'
  }
}

function DisplayUserAvatar(props) {
  const { classes, userImage } = props;
  return userImage ? <Avatar src={userImage} /> : <AccountCircle className={classes.default} />
}

export default withStyles(styles)(DisplayUserAvatar)

This solution was sourced from the following reference: https://material-ui.com/style/icons/

Feel free to reach out if you have any additional inquiries

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

Combining color and typography classes using Tailwind merge is currently not supported

In my custom styling setup, I have created a custom color class called text-green (colors) and a custom typography class called text-card-highlight (utilities), which includes font size and weight attributes. However, when using tailwind-merge, only one of ...

What is preventing my JavaScript variable from being defined outside of this function?

I am currently working on a script where I need to retrieve the dimensions of an image for later use. While directly selecting the img element with jQuery is convenient and efficient, it does not work smoothly in webkit browsers as the image may not be loa ...

Is there a way to achieve the same task with Ajax?

Hey there, I am new to the world of JavaScript and AJAX. I have been reading about how to convert a client-side JavaScript variable into a server-side PHP variable by using AJAX. Can someone please provide me with a code snippet using AJAX for this purpose ...

Attempting to conceal a div element along with its contents using AngularJS

I am attempting to use AngularJS to hide a div and its contents. I have defined a scope variable initialized as false and passed it to the div in order to hide it. However, the div is still visible along with its content. <script type="text/javascr ...

Adjust the text size in a collapsible tree based on the number of expanded components inside the container

Hi there, I'm currently developing a code for an expandable/collapsible tree using checkboxes. I have successfully implemented the basic functionality, but I have a specific requirement. I want the text size to decrease as the tree expands to prevent ...

Including Parameters in File Paths with ExpressJS

I am currently facing an issue with uploading specific photos to a client's folder within my public/assets directory. The file path I am aiming for is public/assets/:id. However, when running my code, the file path always ends up being public/assets/u ...

Steps for retrieving a file after uploading it with express and multer

I successfully uploaded the file to my backend filesystem using multer My server runs on node while the client is based on react. However, I am facing issues with downloading and displaying the saved file on the client side using react. Every time I att ...

Tips for holding off until the fetch operation is complete

Recently started working with JavaScript and facing an issue where the return part doesn't wait for my status from the fetch call. How can I delay this until the status is retrieved? Even tried adding timeouts but that didn't solve the problem. c ...

The alignment of the point on the Highchart line appears to be off

I've encountered a minor issue while using Highstock type Highchart for my statistics. I am utilizing the angular version of highchart. Take a look at the screenshot below: https://i.sstatic.net/HPCkw.png As evident from the image, the point is not ...

Is it feasible to transform HTML into EditorJS JSON format?

Is there a way to display an HTML string in editorJS? Show me from the following code example: <h1>The video src attribute</h1><video width="320" height="240" controls src="movie.ogg">Yourbrowser does not su ...

What sets BoxBufferGeometry apart from BoxGeometry in Three.js?

I'm currently diving into the world of Three.js and I've hit a roadblock trying to distinguish between BoxBufferGeometry and BoxGeometry. Can anyone shed some light on this for me? ...

When integrating Material-UI with REDUX, I am experiencing difficulties in retrieving the most recent value from the global state

Is there a way to update the isAuthenticated state when Material-UI code is involved? I need the Navbar to display different options based on whether the user is authenticated or not. import React from "react"; import clsx from "clsx"; import { withStyles ...

Kurento's WebRTC feature is currently experiencing difficulties with recording functionality

Currently, I am attempting to capture video using the Kurento Media Server with nodejs. Following the hello-world example provided here, I connected a recorderEndpoint to the webrtcEndpoint and successfully got everything up and running. However, on the se ...

What is the best way to align flexbox to the left?

I'm having trouble aligning the FileCard component to the start of the container. I attempted using flex-start in my styling, but the FileCards are still centered. This is the current code snippet: <div v-if="posts" ...

Optimal guidelines for logging in

After successfully creating a website using HTML, CSS, and JavaScript, I am now looking to implement a login feature to provide privacy for certain users. I have noticed that some developers use PHP, while others use JS or Node.js for this purpose. However ...

Pass an array of links from the parent component to the child component in Vue in order to generate a dynamic

I am currently working on building a menu using vue.js. My setup includes 2 components - Navigation and NavLink. To populate the menu, I have created an array of links in the App.vue file and passed it as props to the Navigation component. Within the Navig ...

width restriction to only accommodate on two lines

Whether my text is short or long, I always want to ensure that it remains within two lines by defining the width of my div. Short Text +-------------------------+ | Short text goes here | | Short text goes here | +---------------------- ...

Interested in mastering BootStrap for Angularjs?

As a PHP developer with a newfound interest in JavaScript, I took it upon myself to learn AngularJS and jQuery. However, I recently discovered that simply mastering Angular is not enough - Bootstrap is also necessary. My only issue is my fear of CSS; handl ...

function to trigger lazy loading with jQuery and callback function

Currently, I am triggering a function when lazyload renders an image, which is working well. However, the issue arises when I need to access $(this) within the function. Is there a way to pass this data to the function? You can view the code in action on ...

Finding out the specific row that was clicked in a Jquery Mobile listview

I've searched everywhere and can't find a solution. How can I retrieve the value of a row tapped in a listview? This could be anything from the name to the index within the object. Currently, I have a function handling the tap event. I need to pa ...