score:2
Move your file in public folder and try this command:
<RecommendItem imgUrl={process.env.PUBLIC_URL + '/banner.png'} >
score:0
import styled from 'styled-components';
// used as
<Heading image={require('./images/headerBackground.jpg')} />
// a styled component
const Container = styled.div``
// the class made as
class Heading extends Component {
render() {
return (
<Container image={this.props.backgroundimage}>
<Logo />
<Navigation />
<HeadingBox />
</Container>
)
}
}
export default Heading;
score:0
You may create next.config.js in project's root dir and add this code:
const webpack = (config, options) => {
config.module.rules.push({
test: /\.(png|jpe?g|gif)$/i,
loader: 'file-loader',
options: {
// name: '[path][name].[ext]',
name() {
// `resourcePath` - `/absolute/path/to/file.js`
// `resourceQuery` - `?foo=bar`
if (process.env.NODE_ENV === 'development') {
return '[path][name].[ext]';
}
return '[contenthash].[ext]';
},
publicPath: `/_next/static/images`,
outputPath: 'static/images',
limit: 1000,
},
});
return config
}
module.exports = { webpack }
Do not forget install file-loader if not installed yet.
yarn add -D file-loader
Then in your component you may do like this:
const imgUrl = require("../../static/banner.png").default;
<RecommendItem imgUrl={imgUrl} >
Will be used abs url from bandles.
score:0
You should let the function in the background-image value return a string, url(your_image_link).
<RecommendWrapper>
<RecommendItem imgUrl="img_link" >
</RecommendItem>
</RecommendWrapper>
export const RecommendItem = styled.div`
width: 280px;
height: 50px;
background-size: contain;
background: ${(props) => `url(${props.imgUrl})`};
score:45
React, Gatsby and Next.js all generally use Webpack, which compiles your site before deployment. They will minify imagery and change each image's path.
In order to get images to load properly, you have to reference them dynamically as an import. This way, the variable URL will still work after Webpack does its thing.
import styled from 'styled-components';
import img from './img/bg.gif';
const Content = styled.div`
border: 1px solid #000;
background-image: url(${img});
width: 2000px;
height: 2000px;
`;
Source: stackoverflow.com
Related Query
- how to change image src using props with styled component and react
- Change background image with styled-component
- how to change the antd carousel dot shape and color with styled component
- How Can I Change Next.Js Image Component src with animation?
- React - Using Conditional Rendering with window.location.href to change Background image
- Change background image on React component hover
- How to change background image of specific div with 'onclick' function in React.JS
- React - Styled Component change with Click
- React how to change background of button with styled components
- Change root background color with Material-UI theme
- Reusable Gatsby-Image Component with dynamic image sources
- ReactJS change background image dynamically?
- How do I trigger the change event on a react-select component with react-testing-library?
- Gatsby: Set background image with CSS
- Test styled component with Jest-Enzyme
- React testing component prop change with enzyme
- Overriding react components styles with styled component
- change Url when component renders with react-router-dom?
- Enzyme render styled component with theme
- Styling Material-UI Drawer component with Styled Components
- Share the same styles between two types of component with React Styled Components
- How to add properties to styled component with Typescript?
- Change the background color of a screen with react native
- Why do my styled component keyframes error with ts-styled-plugin(9999) in react when using percentage instead of TO/FROM
- React.forwardRef Causes Error with Styled Component
- Adding active class with styled component not working
- Typescript with Styled component conditional css for custom props
- react, css) transition css for fade in and out, when background image is changed with className
- How to create an image background fade when mapping an array of images with React
- How to change only one component when switching pages with nextjs?
More Query from same tag
- Why can't the object be read?
- How to style the header of react-admin Datagrid?
- How can I set the z-index for this select?
- How to auto indent jsx in VSCode
- Managing state when changing backgroundImage
- add an element to a list inside a list in react
- React native view layer
- Render a component only if it is not already redendered
- Error: Invalid Hook call on component list map. ReactJS
- Typescript module: Export interface and all its props
- How do you fix React Hook useEffect has a missing dependency:
- undefined in variable using react componentDidMount
- Is React Router causing my component to render twice?
- Why can't I store my session in Express.js (MERN Stack)?
- Construct React Component from a React Element
- Displaying content from text editors in React App
- React get value from a class
- Cannot read properties of null React api json fetch
- Generate new page after slug
- How to get progressbar when file upload to browser
- Getting error in node js "secret should be set" and in react "npm ERR! code ELIFECYCLE"
- React Apollo graphql enricher not working
- How to change the id only for the newly inserted object without affecting the object with the same initial id that's already inside the array?
- How to delete a value inside of an array in a firestore doc
- Error: Invariant failed: You should not use <Link> outside a <Router> when I try to test my app using localhost
- Access variable made in componentDidMount
- Parent with height: self doesn't have children's height
- Send to child which component child should render - React js
- React apps Localhost broken after installing bootstrap
- Router issue in react js