today i have a question concerning the REACT package styled-components.
What I'm trying to accomplish is to create a styled div-component, which will be used as a parallax-container and has to load a certain image as its background.
Now I could probably just import the image into the file and call it using ${imgComponentName} while being inside the styled.div.
But I'm trying to be able to load the same Parallax-component with multiple images, whose names i want to pass as a prop on calling the Parallax-component like <Parallax bgi="mountain">
.
My current code looks as follows:
import PropTypes from 'prop-types'
import styled from 'styled-components'
import hills from '../static/img/home/hills.jpeg'
import mountain from '../static/img/home/mountain.jpeg'
const bgi = props => props.bgi ? props => props.bgi : "mountain"
export const Parallax = styled.div`
display: flex;
align-items: center;
justify-content: center;
min-height: 80vh;
background-image: url(${bgi});
background-attachment: fixed;
background-size: cover;
flex-direction: column;
`
Parallax.propTypes = {
bgi: PropTypes.oneOf(['mountain', 'hills'])
}
Is there a way of calling the imported image by its name with the string, that was passed as the prop bgi?
Best regards, Patrick
P.S. I'm aware of the extend-syntax, but i wanted to try to solve the problem trough one styled component.