I have problems adding a background image to my react (bootstrap) project.
I already found much information about using url/file loaders to load and encode images as base64 strings and then adding them to the style of components.
However, my base64 encoded background image is not showing. Although loaded correctly.
Similar questions on SO (e.g. 0)hinted to encoding error (e.g. saying the string had to be patted. But I don't change the string the loader returns...
Here is the relevant jsx code:
var background = require('url-loader!./../images/bg_1.jpg')
export default class MainView extends React.Component {
render() {
var s = {
backgroundImage: 'url(' + background + ')',
backgroundSize: "cover",
backgroundRepeat: "no-repeat",
width: "600px",
height: "400px"
}
return (
<div id="MainView" style={s}>
</div>
);
}
}
Here is a screenshot of the resulting DOM:
The screenshot shows patterns in the string, which I find suspicious. The base64 string further ends with an '='. Other SO posts (2) suggested that the string should end with an '=='. However this post was years old and about a bug in chrome. I have also tried firefox and adding an '=' to the end but it doesn't do anything in both browsers.
The loader is added in the webpack config as follows:
{
test: /\.jpg/,
exclude: /node_modules/,
loader: "url-loader"
}
Thanks for your help.