Theme is not defined for attrs(props => ({}))
See original GitHub issueHi guys,
I’m facing an issue which is actual covered by your unit tests (attrs.test.js
) but does not work in my project or in codesandbox. I’m really confused and would be thankful for a hint what could be wrong.
Environment
## System:
- OS: macOS High Sierra 10.13.6
- CPU: x64 Intel(R) Core(TM) i5-3230M CPU @ 2.60GHz
- Memory: 209.28 MB / 8.00 GB
- Shell: 3.2.57 - /bin/bash
## Binaries:
- Node: 11.0.0 - /usr/local/bin/node
- Yarn: 1.12.1 - /usr/local/bin/yarn
- npm: 6.4.1 - /usr/local/bin/npm
- Watchman: 4.9.0 - /usr/local/bin/watchman
## npmPackages:
- babel-plugin-styled-components: >= 1 => 1.8.0
Reproduction
https://codesandbox.io/s/oj4x41x0yz
Steps to reproduce
The actual setup is just a ThemeProvider and a styled components beneath it which accesses the theme via styled.img.attrs(props => ({}))
.
Expected Behavior
It should be possible to access props.theme
with this code since it’s proposed as good practice in the docs: https://www.styled-components.com/docs/faqs#when-to-use-attrs
Actual Behavior
As can be seen in the CodeSandbox snippet the following error occurs (which means theme in undefined):
Cannot read property 'icons' of undefined
/MyIcon.js:4:19
1 | import styled from "styled-components";
2 |
3 | export default styled.img.attrs(props => ({
> 4 | src: props.theme.icons.github
| ^
5 | }))`
My current workaround is adding a withTheme around the styled component 😕
Greetings, Thomas
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Passing props to MUI styles - reactjs - Stack Overflow
Solution for how to use both props and theme in material ui : ... When you're using withStyles , you have access to...
Read more >Object.defineProperty() - JavaScript - MDN Web Docs
A function which serves as a getter for the property, or undefined if there is no getter. When the property is accessed, this...
Read more >Complete Guide On How To Use Styled-components In React
It's a component that is styled not by using a CSS file, ... const Button = styled.button.attrs((props) => ({ borderColor: props.active ?
Read more >Material-UI makeStyles, useStyles, createStyles, and ...
5 MUI makeStyles with Props, Theme, and TypeScript ... React had not yet introduced hooks back when Material-UI 3 was first released.
Read more >Customize a React InstantSearch widget - Algolia
The Highlight and the Snippet widgets take two props: ... so there is no need to import it separately when using the satellite...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fixed in https://github.com/styled-components/styled-components/releases/tag/v4.1.2
There’s a fix in master, it just hasn’t been released yet I think. On Tue, Nov 27, 2018 at 10:17 AM Sébastien Fichot notifications@github.com wrote: