Trying to set a component to a repeating background image of adivI have a pattern that is repeated throughout the website.
Simplified pattern components:
import React from 'react' import PropTypes from 'prop-types' import styled from 'styled-components' const Container = styled.svg` height: ${({ height }) => height}; ` const Path = styled.path` fill: ${({ color }) => color}; ` export default function Pattern({ height, color }) { return ( ) } Pattern.propTypes = { height: PropTypes.string, color: PropTypes.string, } Pattern.defaultProps = { height: '10px', color: 'blue', }
I introduce the modal component and pass it.
Simplified components:
import React from 'react' import PropTypes from 'prop-types' import styled from 'styled-components' // Components import { Pattern } from '../atoms' const Container = styled.div` // Removed ` // HERE const Repeat = styled.div` margin-bottom: ${({ theme }) => theme.space.normal}; width: 100%; background-image: url(${({ background }) => background}); background-repeat: repeat-x; ` export default function SoQues(props) { return ( }/> ) } SoQues.propTypes = { // props }
But for some reason it won't render.
What am I doing wrong and how do I introduce a component and set it to repeat the background image horizontally?
You can render the SVG as a static markup, encode it to Base64 and use it as a URL:
Real-time testing
import * as React from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; export default function App() { const mySvgBase64Encoded = btoa(renderToStaticMarkup( )); return ( ); } const MySvg = () => ( );Using your code, it might look something like the following (I didn't test it)
const Repeat = styled.div` margin-bottom: 20px; width: 100%; background-image: ${({ background }) => `url(data:image/svg+xml;base64,${background})`}; background-repeat: repeat-x; ` export default function App() { const background = btoa(renderToStaticMarkup( )) return ( ) }