score:9
Accepted answer
styled components are still just react components, and you can simply provide a default prop value in the case a src
prop isn't passed when used.
import myimg from './img/icon_my';
const myicon = styled.img`
width: 100%;
`;
myicon.defaultprops = {
src: myimg,
};
usage:
<myicon /> // uses default myimg
<myicon src="/path/to/sourceimage" /> // uses "/path/to/sourceimage"
btw correct attrs
format to accept props would be to define a function taking props and returning an object:
const myicon = styled.img.attrs(props => ({
src: props.img || myimg,
}))`
width: 100px;
`;
Source: stackoverflow.com
Related Query
- how to change image src using props with styled component and react
- how to change the antd carousel dot shape and color with styled component
- How to deal with component reference variables and props in React 0.12+?
- How to mock properly with Jest a React component using render props
- How do you change the color of a React component using props
- How Can I Change Next.Js Image Component src with animation?
- How do I convert a component with a constructor function and props, to a functional component using hooks and props
- Passing React Props with CSS and using them in a component with TypeScript?
- How to access props in react functional component using react and typescript?
- How can I test if child React component was rendered based on a URL change using Jest and Enzyme
- How to get parent props in child component when using styled component in react
- How to use template string to create link from props and render image in React component
- How to mock react component and check if called with props
- How to test a React Component with Props in TypeScript using Jest?
- How can I pass props from one component to another using Link and router in react
- How to pass a json array that comes from backend to child component using props and react hooks?
- How to add an image in the first react component and ignore in rest of the components using map function
- How to test React component with children using jest and enzyme?
- how to customize the shape height and width of a component from the props using react
- Using props and other attributes with react component definition
- How to extract props and states from one sibling component to another with react hooks?
- How do I map dispatch to props within a recursively nested component using React and Redux?
- How to redirect to new component with props on submit using React
- How to pass the props to react functional component using react table and typescript?
- How to dynamically change displayed image using React and Parceljs?
- How do I create image maps (clickable areas on an image using <map >) with React and JSX?
- Why does not the background color get applied from props using state for a styled component div using react and typescript?
- How to evaluate the passed prop to the styled component using react and typescript?
- How to add ref to a react styled component div using react and typescript?
- How to mock a complex react component with props using Jest/ react testing library
More Query from same tag
- Getting Axios Response Data React
- React - calling function from a sibling component
- this.state in constructor call function synchronously
- Toggle between two components /buttons using react toggle hook {Show one component and Hide another}
- ReactJs: fontSize does not change when calling it from a font change method
- How to set state at the server for data from API using react redux
- Problem in React typing text in a field text
- How original data is getting updated on changing the value of an object?
- ReactHooks: Invoke callback when store value changes
- Change image into Blob is not working in IE11
- Filter item on item list that is on state using useState and useEffect
- Reactjs problem rendering binary image retrieved from mongodb
- How to filter array from a stacked array in react
- Reactjs closure when passing state to component
- Show/hide the scrollbar under specific circumstances through CSS
- How to check which options in a multi-select input form are selected from ReactStrap
- Navigate using react-router-dom v6 after performing user action
- Making a typing timer in RxJS; Tracking time spent typing
- React event handling between Child and Parent Component
- Add filter scripts to react app that is using routes?
- react dc.js import not working
- Cookies not being passed from response to next request
- How to best clone a class in ReactJS
- How to define value for useState image in React Native
- How to structure Department-Employee data
- React-dates how to recognize in which way user choose data
- Passing string parameters into function from one component to another in reactjs
- React map changes index number and breaks conditional rendering
- React Ag-grid after you press ENTER in cell edit, move cursor down to next row (like Excel)
- Update modal value with the server response data in React-Semantic UI