emotion: Unknown prop on DOM element
emotionversion: 6.0.3reactversion: 15.6.1
Relevant code:
const Avatar = styled('img')`
width: 96px;
height: 96px;
border-radius: ${props =>
props.rounded ? '50%' : 0};
`
Example above is from https://emotion.sh/.
What you did: Tried to render the example from above using
<Avatar rounded />
What happened:
I got the following error in my console: Unknown prop rounded on tag. Remove this prop from the element.
Problem description: I’m not sure whether this is a problem due to my configuration/implementation or if this is a problem in the library. I assumed this should work without any errors, since i copied the exact example from https://emotion.sh/. However when scanning emotion’s source code i found that styled just blindly merges/spreads all the props onto the DOM element.
Suggested solution: Whitelist the allowed props for each DOM element, which is the way styled-components is doing it at the moment.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 18
- Comments: 37 (15 by maintainers)
Commits related to this issue
- Filter more props that don't apply Closes #183 — committed to emotion-js/emotion by emmatown 7 years ago
My temporary solution:
I also came across that issue, in my case with react-bootstrap’s ListGroupItem, which also passes through all the props.
What do you think about an official
omitPropsoption to styled?My current workaround doesn’t feel right nor readable:
I could take a stab on a PR if you like that omitProps proposal.
I’m not familiar with
grid-styled/emotion, but looking at the code I would guess thatvariant,justifyContentandalignItemsare recognised props for theirFlexAPI, and therefore they will take care of ensuring these don’t propagate to the underlying DOM.However,
isOpenis something I assume is what you have added and is thus not recognised by thegrid-styled/emotionFlexcomponent. Therefore,styled(fromreact-emotion) will pass everything through it doesn’t recognise – as it doesn’t know if you are wrapping a base DOM node, or a custom component, and therefore can’t know when or what to prevent propagating through.However, you can manually tell
styledwhich props is should propagate and which it shouldn’t, although the functionality doesn’t seem to be well documented yet: See https://github.com/emotion-js/emotion/issues/655 for more info.To fix the warning here, I think the following should do the job:
I don’t know if this is a ‘good’ solution but I’ve been using the following and getting by without any errors:
edit
The following helper function can help if the above is a little tedious:
You could also have the
tagbe curried if you didn’t want to pass that it (or if you make a lot of the same type of component)Perfect thank you! For anybody stumbling across this issue, take a look at #655.
A simple workaround I’ve found is to just prefix props only used for styles with
data-. This will prevent the error being thrown.It’s not ideal, but it works.
Sounds like this should be fixed looking at the above, but with emotion 9.0.1 I still get an error e.g. with a
div:Any advice? I am using this with
react-staticif that could make any difference…That’s actually a pretty good solution, @divyagnan. However i would like to see this solved from the library instead. Currently i’m just directly using the css() function in my components instead of using styled(), passing the relevant props to css() and composing a style object (resulting in classnames) myself.
Any update about this? The code below is just an example:
Since
leftElementis not a valid prop, I cannot access this prop aftershouldForwardPropvalidation, but if I remove it, I receive theReact does not recognize the leftElement prop on a DOM element.warning.This feature already is in emotion - check out shouldForwardProp
great stuff @divyagnan
FWIW you can use recompose’s
mapPropshttps://github.com/acdlite/recompose/blob/master/docs/API.md#mapprops
@ai I’ve been writing more and more emotion code and thinking on how I want to handle this in the core. Right now I’m using this setup.
and usage
I’ve come to really like this pattern but I’m not sure how to make this a solution that works for everyone.
@thangngoc89 uh, so you clean all non-DOM props automatically? Awesome!
@tkh44 Wow, it was fast! I like Emotion community 😃.
Also, we may need docs for it since it is not clear.
I’ve been using glamor jsxstyle recently with some custom options that allow me to set global custom style props to strip out things like
marginVertical,elevation, etc… similar to what you have above. Maybe you could have something similar. Some way to set global props or something. Could it be a babel option? Or maybe you create your ownemotion? Super naive example:@ai Sorry I was on my phone. It’s like this