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.

`as` prop causes TypeError when wrapping components

See original GitHub issue

Current behavior:

as prop causes TypeError for components utilizing that prop wrapped in styled()

To reproduce:

https://codesandbox.io/s/quizzical-kalam-xvgve?from-embed=&file=/src/App.tsx

Expected behavior:

I guess CreateStyled shouldn’t extend props with as prop for all cases extending components. (only for IntrinsicElements)

Environment information:

  • react version: latest
  • @emotion/react version: latest

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
Andaristcommented, Feb 3, 2021

yeah, that’s the current behaviour, but I don’t think it is possible to conditionally change the TypeScript typing based on passing something like shouldForwardProp: prop => prop !== ‘as’.

Yeah, this is impossible/hard. If one wants to add support for as prop for their wrapped custom components then they can do this manually.

The only solution I can think of right now is not adding the as prop to the type when wrapping composite components, but that will result in wrong typing when you pass the shouldForwardProp as above.

That’s fine - this should usually be a rare situation and the full relation between input props, shouldForwardProp and the forwarded props is not validated anyway.

So I suggest removing the as prop from type definitions for composite components completely. Any thoughts?

That aligns with my current way of thinking about this - please only make sure to add some basic tests for the expected behavior.

0reactions
wolszczak96commented, Feb 3, 2021

Not sure what kind of fix you have in mind. 😅

if you provide shouldForwardProp that doesn’t forward the as prop

yeah, that’s the current behaviour, but I don’t think it is possible to conditionally change the TypeScript typing based on passing something like shouldForwardProp: prop => prop !== 'as'.

I could do that utilising some additional prop, like shouldForwardAsProp, but it’s stupid.

The only solution I can think of right now is not adding the as prop to the type when wrapping composite components, but that will result in wrong typing when you pass the shouldForwardProp as above.

But on the other hand, are there any real-life examples of needing to render composite component as a html tag? You lose all the functionality of the wrapped component, the typing is probably wrong in most cases, as your wrapped component is probably using some fancy props, which are not applicable to html tags.

So I suggest removing the as prop from type definitions for composite components completely. Any thoughts?

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - Why does Route with children as components cause ...
When rendering on the component prop, the route props are passed along, so props.match.params works, but now you can't pass the additional ...
Read more >
HOC props are required to be present in wrapped component ...
When using the new way of typing HOC's in v0.53.0, it is required that all injected properties are present in the Props definition...
Read more >
Why do we have to wrap React components? | by Aaron S
Syntax error: Adjacent JSX elements must be wrapped in an enclosing ... elementType — The HTML Tag you want to display; props —...
Read more >
A better way of solving prop drilling in React apps
In the spirit of "using the platform," learn how the React library provides a workaround for prop drilling without Redux or the Context...
Read more >
Error Boundaries - React
In the past, JavaScript errors inside components used to corrupt React's internal state and cause it to emit cryptic errors on next renders....
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