Skip to content

Render component from prop #181

Closed Answered by ben-rogerson
matthisk asked this question in General
Discussion options

You must be logged in to vote

Doesn't look like it's possible - styled-components is hoisting the component out of the BaseButton component making Component undefined.

Here's the compiled code:

var _StyledComponent = _styled(Component).withConfig({ // < Component is hoisted up here
  // sc props
})({
  "borderRadius": "9999px"
});

const BaseButton = ({
  component: Component = 'button',
  children,
  ...props
}) => React.createElement(_StyledComponent, props, children);

Emotion doesn't have the same behaviour:

const BaseButton = ({
  component: Component = 'button',
  children,
  ...props
}) => React.createElement(Component, _extends({
  css: {
    "borderRadius": "9999px"
  }
}, props), children);

Alternatives

If it…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ben-rogerson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants