Sounds complicated but it really isn't once you see what i'm trying to do. Yes, I've read the react documentation about this (https://reactjs.org/docs/dom-elements.html#style) and seen many examples, but none answer my question.
Here is my react code:
import React, { useState, useEffect } from 'react';
import {Route, Link} from 'react-router-dom';
import './box_link.css'
function Box_Links(props) {
const [url, setUrl] = useState('');
useEffect(() => {
setUrl(
{
background-image: 'linear-gradient(rgba(211, 211, 211, .7), rgba(211, 211, 211, .7)), url("https://freshmommyblog.com/wp-content/uploads/2020/11/cinnamon-rolls-400-cropped.jpg")',
background-size: 'cover',
}
);
return (
<Link style={url}
className='box_links'
to='/box-links'>
<div className='box_label' >
<h3>Recipes</h3>
</div>
</Link>
);
}
export default Box_Links;
This is the trouble making line:
{
background-image: 'linear-gradient(rgba(211, 211, 211, .7), rgba(211, 211, 211, .7)), url("https://freshmommyblog.com/wp-content/uploads/2020/11/cinnamon-rolls-400-cropped.jpg")',
background-size: 'cover'
}
This is the error I keep getting (the carrot '^' is actually under the 'r' in 'rolls':
\box_link.js: Unexpected token, expected "," (11:26)
9 | setUrl(
10 | {
> 11 | background-image: 'linear-gradient(rgba(211, 211, 211, .7), rgba(211, 211, 211, .7)), url("https://freshmommyblog.com/wp-content/uploads/2020/11/cinnamon-rolls-400-cropped.jpg")',
| ^
12 | background-size: 'cover',
13 | }
14 | );
I've played around with the placing of the commas and the syntax, for the life of me I can't get this to work and when I do, the background image doesn't display, any ideas or tips?
EDIT/ANSWER: Patrick Roberts provided the correct answer; however, his answer has one flaw (the initial value passed to use state) so I'm showing his revised answer below:
import { useEffect, useState } from 'react';
import { Link } from 'react-router-dom';
import './box_link.css';
function Box_Links(props) {
const [url, setUrl] = useState({});
useEffect(() => {
setUrl({
'background-image': 'linear-gradient(rgba(211, 211, 211, .7), rgba(211, 211, 211, .7)), url("https://freshmommyblog.com/wp-content/uploads/2020/11/cinnamon-rolls-400-cropped.jpg")',
'background-size': 'cover',
});
}, []);
return (
<Link
style={url}
className="box_links"
to="/box-links"
>
<div className="box_label">
<h3>Recipes</h3>
</div>
</Link>
);
}
export default Box_Links;