Skip to content

Commit 0120e9c

Browse files
committed
Add Dynamically Create HTML Elements as a react til
1 parent 07a3884 commit 0120e9c

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
1010
For a steady stream of TILs from a variety of rocketeers, checkout
1111
[til.hashrocket.com](https://til.hashrocket.com/).
1212

13-
_672 TILs and counting..._
13+
_673 TILs and counting..._
1414

1515
---
1616

@@ -465,6 +465,7 @@ _672 TILs and counting..._
465465
- [Destructure Variables As Props To A Component](react/destructure-variables-as-props-to-a-component.md)
466466
- [Dispatch Anywhere With Redux](react/dispatch-anywhere-with-redux.md)
467467
- [Dynamically Add Props To A Child Component](react/dynamically-add-props-to-a-child-component.md)
468+
- [Dynamically Create HTML Elements](react/dynamically-create-html-elements.md)
468469
- [Enforce Specific Values With PropTypes](react/enforce-specific-values-with-proptypes.md)
469470
- [Force A Component To Only Have One Child](react/force-a-component-to-only-have-one-child.md)
470471
- [Inactive And Active Component Styles With Radium](react/inactive-and-active-component-styles-with-radium.md)
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Dynamically Create HTML Elements
2+
3+
An HTML element can be created with a string that matches a recognized
4+
entity.
5+
6+
```javascript
7+
const Paragraph = 'p';
8+
return <Paragraph>Some paragraph content</Paragraph>
9+
```
10+
11+
This means we can dynamically create HTML elements such as headers:
12+
13+
```javascript
14+
const H = ({ level, ...props }) => {
15+
const Heading = `h${Math.min(level, 6)}`;
16+
return <Heading {...props} />;
17+
};
18+
19+
return (
20+
<React.Fragment>
21+
<H level={1}>Header 1</H>
22+
<H level={2}>Header 2</H>
23+
<H level={5}>Header 5</H>
24+
</React.Fragment>
25+
);
26+
```
27+
28+
With some
29+
[inspiration](https://medium.com/@Heydon/managing-heading-levels-in-design-systems-18be9a746fa3),
30+
I've created a [live example here](https://codesandbox.io/s/3v202wmmy1).

0 commit comments

Comments
 (0)