Can withStyles be used on the result of a function?











up vote
1
down vote

favorite
1












I'm currently trying to apply additional styling to the result of a function that returns one of two components depending on the presence of a specific set of props, as such:



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 />
}


However, when I apply withStyles to the result, none of the additional styling appears. Am I approaching this the right way?



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} />
...
)
}









share|improve this question







New contributor




jjnotjayjay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    1
    down vote

    favorite
    1












    I'm currently trying to apply additional styling to the result of a function that returns one of two components depending on the presence of a specific set of props, as such:



    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 />
    }


    However, when I apply withStyles to the result, none of the additional styling appears. Am I approaching this the right way?



    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} />
    ...
    )
    }









    share|improve this question







    New contributor




    jjnotjayjay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I'm currently trying to apply additional styling to the result of a function that returns one of two components depending on the presence of a specific set of props, as such:



      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 />
      }


      However, when I apply withStyles to the result, none of the additional styling appears. Am I approaching this the right way?



      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} />
      ...
      )
      }









      share|improve this question







      New contributor




      jjnotjayjay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I'm currently trying to apply additional styling to the result of a function that returns one of two components depending on the presence of a specific set of props, as such:



      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 />
      }


      However, when I apply withStyles to the result, none of the additional styling appears. Am I approaching this the right way?



      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} />
      ...
      )
      }






      javascript css reactjs material-ui






      share|improve this question







      New contributor




      jjnotjayjay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      jjnotjayjay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      jjnotjayjay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 5 at 2:06









      jjnotjayjay

      61




      61




      New contributor




      jjnotjayjay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      jjnotjayjay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      jjnotjayjay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          try this way



          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 UserAvatar(props) {
          const { classes, userImage } = props;
          return userImage ? <Avatar src={userImage} /> : <AccountCircle className={classes.default} />
          }

          export default withStyles(styles)(UserAvatar)


          I found this from the documentation: https://material-ui.com/style/icons/



          please let me know if you have any further questions






          share|improve this answer





















          • Thanks for responding. However, I'm trying to use the result of UserAvatar without additional margin styling in some situations, and only apply the addtl. margin styling in others (UserAvatarHeader). It appears your solution would apply the styling every time UserAvatar is rendered.
            – jjnotjayjay
            Nov 5 at 18:17











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          jjnotjayjay is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147407%2fcan-withstyles-be-used-on-the-result-of-a-function%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          try this way



          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 UserAvatar(props) {
          const { classes, userImage } = props;
          return userImage ? <Avatar src={userImage} /> : <AccountCircle className={classes.default} />
          }

          export default withStyles(styles)(UserAvatar)


          I found this from the documentation: https://material-ui.com/style/icons/



          please let me know if you have any further questions






          share|improve this answer





















          • Thanks for responding. However, I'm trying to use the result of UserAvatar without additional margin styling in some situations, and only apply the addtl. margin styling in others (UserAvatarHeader). It appears your solution would apply the styling every time UserAvatar is rendered.
            – jjnotjayjay
            Nov 5 at 18:17















          up vote
          0
          down vote













          try this way



          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 UserAvatar(props) {
          const { classes, userImage } = props;
          return userImage ? <Avatar src={userImage} /> : <AccountCircle className={classes.default} />
          }

          export default withStyles(styles)(UserAvatar)


          I found this from the documentation: https://material-ui.com/style/icons/



          please let me know if you have any further questions






          share|improve this answer





















          • Thanks for responding. However, I'm trying to use the result of UserAvatar without additional margin styling in some situations, and only apply the addtl. margin styling in others (UserAvatarHeader). It appears your solution would apply the styling every time UserAvatar is rendered.
            – jjnotjayjay
            Nov 5 at 18:17













          up vote
          0
          down vote










          up vote
          0
          down vote









          try this way



          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 UserAvatar(props) {
          const { classes, userImage } = props;
          return userImage ? <Avatar src={userImage} /> : <AccountCircle className={classes.default} />
          }

          export default withStyles(styles)(UserAvatar)


          I found this from the documentation: https://material-ui.com/style/icons/



          please let me know if you have any further questions






          share|improve this answer












          try this way



          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 UserAvatar(props) {
          const { classes, userImage } = props;
          return userImage ? <Avatar src={userImage} /> : <AccountCircle className={classes.default} />
          }

          export default withStyles(styles)(UserAvatar)


          I found this from the documentation: https://material-ui.com/style/icons/



          please let me know if you have any further questions







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 5 at 13:19









          Nadun

          978610




          978610












          • Thanks for responding. However, I'm trying to use the result of UserAvatar without additional margin styling in some situations, and only apply the addtl. margin styling in others (UserAvatarHeader). It appears your solution would apply the styling every time UserAvatar is rendered.
            – jjnotjayjay
            Nov 5 at 18:17


















          • Thanks for responding. However, I'm trying to use the result of UserAvatar without additional margin styling in some situations, and only apply the addtl. margin styling in others (UserAvatarHeader). It appears your solution would apply the styling every time UserAvatar is rendered.
            – jjnotjayjay
            Nov 5 at 18:17
















          Thanks for responding. However, I'm trying to use the result of UserAvatar without additional margin styling in some situations, and only apply the addtl. margin styling in others (UserAvatarHeader). It appears your solution would apply the styling every time UserAvatar is rendered.
          – jjnotjayjay
          Nov 5 at 18:17




          Thanks for responding. However, I'm trying to use the result of UserAvatar without additional margin styling in some situations, and only apply the addtl. margin styling in others (UserAvatarHeader). It appears your solution would apply the styling every time UserAvatar is rendered.
          – jjnotjayjay
          Nov 5 at 18:17










          jjnotjayjay is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          jjnotjayjay is a new contributor. Be nice, and check out our Code of Conduct.













          jjnotjayjay is a new contributor. Be nice, and check out our Code of Conduct.












          jjnotjayjay is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147407%2fcan-withstyles-be-used-on-the-result-of-a-function%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini