-
-
Save merapi/cde36c0982e2c9f5e1833aaf659bd72a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This doesn't work | |
| // I'd like to figure out why it doesn't. | |
| // I get the following error: | |
| // Heading(...): A valid React element (or null) must be returned. | |
| // You may have returned undefined, an array or some other invalid object. | |
| import styled from 'styled-components'; | |
| export const getFontSize = ({ level }) => `${0.5 + (1 / level)}rem`; | |
| const Heading = ({ level }) => { | |
| const element = `h${level}`; | |
| console.log('element', element); | |
| return styled(element)` | |
| font-family: ${(props) => props.theme.fonts.primary}; | |
| font-weight: 500; | |
| font-size: ${getFontSize}; | |
| `; | |
| }; | |
| Heading.defaultProps = { | |
| level: 1 | |
| }; | |
| export default Heading; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This works, but I'd rather avoid the extra boilerplate if I can | |
| import React from 'react'; | |
| import styled, { css } from 'styled-components'; | |
| export const getFontSize = ({ level }) => `${0.5 + (1 / level)}rem`; | |
| const styles = css` | |
| font-family: ${(props) => props.theme.fonts.primary}; | |
| font-weight: 500; | |
| font-size: ${getFontSize}; | |
| `; | |
| const Heading = styled( | |
| ({ level, children, ...props }) => | |
| React.createElement(`h${level}`, props, children) | |
| )`${styles}`; | |
| Heading.defaultProps = { | |
| level: 1 | |
| }; | |
| export default Heading; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment