简体   繁体   中英

How do I set a background image from url in React (an outside url not a local file)

I have the following code:

const UploadedImage = ({
    imageKey, 
    url, 
    clickHandler, 
    deleteButtonHandler, 
    isSelected}) => {
    return (
        <button 
            className="placeholder-button rounded" 
            type="button"
            style={{backgroundImage: url({url})}}
            onClick={() => clickHandler(imageKey, url)}
        >
            {isSelected && <div className="placeholder-overlay rounded" />}
            <UtilityButton 
                className="delete-uploaded-image-utility-button" 
                variant="delete"
                clickHandler={() => deleteButtonHandler(imageKey)}
            />
        </button>
    );
}

I am trying to set a background image in the button using a url from the web (not a local file). How do I do this? Current code doesn't work.

backgroundImage is expecting a string value which you currently aren't passing . You should change it to

style={{ backgroundImage: `url(${url})` }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM