I rarely find a problem that I can't already find an answer to, but this time I'm really stumped - and I'm sorry in advance if my question will prove silly or trivial.
I'm building a simple React app, and I'm supposed to give the Home component a background image, which is an .svg
file. However, the image does not load - dev tools Network section says it could not load the image - but it does show up there, with a hashed filename, so I'm guessing the file-loader works. I'm also using react-css-modules. I've tried a couple approaches:
React Component v.1
import React from 'react'
import CSSModules from 'react-css-modules'
import styles from '../styles/home.css'
class Home extends React.Component {
render() {
return (
<div className='homeContainer' styleName='homeBg'>
<div className='col-sm-12 text-center'>
<div className="jumbotron col-sm-6 col-sm-offset-3" styleName='transparentBg'>
<h1>Enter Your Location</h1>
<form>
<div className="form-group">
<input type="text" className="form-control" placeholder='Location...' />
</div>
<div className="form-group">
<button className='btn btn-lg btn-submit btn-success' type="submit">Continue</button>
</div>
</form>
</div>
</div>
</div>
);
}
}
export default CSSModules(Home, styles)
Here's the CSS Module:
.homeBg {
background-image: url(../images/pattern.svg)
}
...
And my Webpack config:
loaders: [
{test: /\.js$/, exclude: '/node_modules/', loader: 'babel-loader'},
{test: /\.css$/,
loaders: [
'style?sourceMap',
'css?modules&importLoaders=1&localIdentName=[path]___[name]__[local]___[hash:base64:5]'
]
},
{test: /\.svg$/, loader: 'file-loader'}
]
Here's the second approach I tried in the component:
render() {
const bgImg = require('../images/pattern.svg')
const background = {
backgroundImage: 'url(' + bgImg + ')'
}
return (
<div className='homeContainer' style={background}>
...
But whetever I do - the image does not load. It loads in a regular <img>
tag, but not as a background. What else could I try? Thanks in advance for any suggestions!