question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Theme is not defined for attrs(props => ({}))

See original GitHub issue

Hi 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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
probablyupcommented, Nov 27, 2018

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:

Same here.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/styled-components/styled-components/issues/2245#issuecomment-442095638, or mute the thread https://github.com/notifications/unsubscribe-auth/AAiy1m0ruA6tVqNnMIBIIdupj-NsIFw1ks5uzVeEgaJpZM4YzRLd .

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found