|
1 | | -# React from Markup |
| 1 | +# react-from-markup |
| 2 | +> Convert static markup into React components, so you can control React props in a static environment. |
2 | 3 |
|
3 | | -> Declare your React components with static HTML |
| 4 | +## Intro |
| 5 | +`react-from-markup` converts this |
4 | 6 |
|
5 | | -## Why? |
| 7 | +```html |
| 8 | +<div |
| 9 | + data-rehydratable="ShowMore" |
| 10 | + data-content="Hello, World!" |
| 11 | +> |
| 12 | + Hello, World! |
| 13 | +</div> |
| 14 | +``` |
6 | 15 |
|
7 | | -Sometimes, your tech stack doesn’t speak JavaScript very well. |
| 16 | +into this |
8 | 17 |
|
9 | | -This can be a particular problem with some CMS, or legacy systems. React is a JavaScript library, and oftentimes, it’s difficult to create the bootstrapping JavaScript from your templating system. |
| 18 | +```jsx |
| 19 | +<ShowMore content="Hello, World!" /> |
| 20 | +``` |
10 | 21 |
|
11 | | -`react-from-markup` is intended to make it possible to use React components on these legacy systems, _without changing the way you write your React components_. It provides tools to simplify the mapping from `data-` attributes into React props, and _can even handle React children_. |
| 22 | +using a `rehydrator`: |
12 | 23 |
|
13 | | -The result: React can be used to build a component library, useable by other React apps, but you don’t need to write a second component library for your legacy systems, or a second set of non-React JavaScript. You just need to integrate new markup into your non-React templates, and run a script on page load to initialize. |
| 24 | +```jsx |
| 25 | +import ShowMore from "./ShowMore"; |
14 | 26 |
|
15 | | -## Notable uses |
| 27 | +export default async domNode => { |
| 28 | + return <ShowMore |
| 29 | + content={domNode.getAttribute("data-content")} |
| 30 | + />; |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +This is extremely useful for changing React props in a non-React environment, like a CMS with it’s own templating language. |
| 35 | + |
| 36 | +## Features |
| 37 | +* Convert static markup into React components |
| 38 | +* Rehydrate React children |
| 39 | +* Asynchronous rehydrators |
| 40 | +* … |
| 41 | + |
| 42 | +## Quick start |
| 43 | +Install `react-from-markup` |
| 44 | + |
| 45 | +```sh |
| 46 | +yarn add react-from-markup |
| 47 | +``` |
| 48 | + |
| 49 | +Annotate your component’s markup with `data-` attributes for each prop, and a `data-rehydratable` attribute to let `react-from-markup` identify your component. |
| 50 | + |
| 51 | +```jsx |
| 52 | +// components/ShowMore/index.js |
| 53 | +export default ({ content }) => { |
| 54 | + <div data-content={content} data-rehydratable="ShowMore"> |
| 55 | + {content} |
| 56 | + </div> |
| 57 | +} |
| 58 | +``` |
16 | 59 |
|
17 | | -- Thomson Reuters uses `react-from-markup` on its Enterprise Web Platform. It runs on Adobe Experience Manager and HTL, and powers thomsonreuters.com, business unit sites, and campaign pages. It’s also used on their blog network, a WordPress-based platform of blogs. The same React-based component library is able to back each platform, and is also used in several non-`react-from-markup` sites that were able to use React directly, such as the [2017 Annual Report](https://ar.tr.com). |
| 60 | +Write a rehydrator for your component, to describe how to map static data back to React props: |
18 | 61 |
|
19 | | -## Documentation |
| 62 | +```jsx |
| 63 | +// components/ShowMore/rehydrator.js |
| 64 | +import ShowMore from "./ShowMore"; |
20 | 65 |
|
| 66 | +export default async domNode => { |
| 67 | + return <ShowMore |
| 68 | + content={domNode.getAttribute("data-content")} |
| 69 | + />; |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +Run `react-from-markup` on page load |
| 74 | + |
| 75 | +```js |
| 76 | +// index.js |
| 77 | +import rehydrate from "react-from-markup"; |
| 78 | +import showMoreRehydrator from "./components/ShowMore/rehydrator"; |
| 79 | + |
| 80 | +rehydrate(document.getElementById("root"), { |
| 81 | + ShowMore: showMoreRehydrator |
| 82 | +}); |
| 83 | +``` |
| 84 | + |
| 85 | +## Full Documentation |
21 | 86 | In-depth documentation can be found [here](https://simon360.github.io/react-from-markup). |
| 87 | + |
| 88 | +## Notable uses |
| 89 | +- [Thomson Reuters](thomsonreuters.com) uses `react-from-markup` on its Enterprise Web Platform. It runs on Adobe Experience Manager and HTL, and powers thomsonreuters.com, business unit sites, and campaign pages. It’s also used on their blog network, a WordPress-based platform of blogs. The same React-based component library is able to back each platform, and is also used in several non-`react-from-markup` sites that were able to use React directly, such as the [2017 Annual Report](https://ar.tr.com). |
0 commit comments