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.

new attrs as a function doesn't pass theme to the function provided

See original GitHub issue

Environment

  • React-native IOS

System:

  • OS: macOS High Sierra 10.13.6
  • CPU: x64 Intel® Core™ i5-5350U CPU @ 1.80GHz
  • Memory: 43.83 MB / 8.00 GB
  • Shell: 5.6.2 - /usr/local/bin/zsh

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:

  • styled-components: ^4.1.1 => 4.1.1

Reproduction

  1. create a global theme and provide it using <ThemeProvider>.
  2. create a styled component with attrs, where the function expects a theme to be in the props. (eg, `styled.div.attrs(props => ({ backgroundColor: props.theme.bgcolor}))```)

Expected Behavior

theme is provided to the props of the attrs function

Actual Behavior

theme is not provided and the app crashes with “cannot read property ‘bgcolor’ of undefined”.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

15reactions
JereLimacommented, Oct 27, 2021

Hi guys, I solved it in the following way:

export const TextInput = styled.TextInput.attrs(({theme}) => ({ placeholderTextColor: theme.colors.primary, })) font-family: ${({ theme }) => theme.fonts.interBold}; font-size: 18px; color: ${({ theme }) => theme.colors.primary}; ;

1reaction
probablyupcommented, Nov 13, 2018

@NProtagoras looking into this now, thanks!

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 : ... function SimpleCard(props) { // in demo.js const { classes,...
Read more >
API Reference - styled-components
A function that receives the props that are passed into the component and computes a value, that is then going to be merged...
Read more >
attr() - CSS: Cascading Style Sheets - MDN Web Docs
The attr() CSS function is used to retrieve the value of an attribute of the selected element and use it in the stylesheet....
Read more >
Introducing script attributes related functions in WordPress 5.7
WordPress 5.7 adds a handful of new functions that enables passing attributes, such as async or nonce , to both regular and inline...
Read more >
API Reference - attrs 22.2.0 documentation
This works similarly to attr.evolve but that function does not work with ... It is given the passed-in value, and the returned value...
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