Search code examples
javascriptreactjsmaterial-uirequireavatar

How pass arguments via require to material avatar


import Avatar from '@material-ui/core/Avatar';

This code works:

<Avatar alt="user 4" src={require('Assets/img/user-1.jpg')} className="size-80 rounded-circle border-info rct-notify" />

But I need to pass variable, this code doesn't work :

{line.icon != null && <Avatar alt="user 3" src={require(line.icon)} className="size-80 rounded-circle border-warning rct-notify" />}

Error:

Uncaught Error: Cannot find module 'Assets/img/user-3.jpg'

Solution

  • I find some case how it works, but still not enough.

    This code works only if in require we start from some text and after this, we can use a variable.

    {line.icon != null && <Avatar alt="user 3" src={require(`Assets/img/${line.icon}`)} className="size-80 rounded-circle border-warning rct-notify" />}