{"id":21100457,"url":"https://github.com/deschtex/html2react","last_synced_at":"2025-07-28T05:05:48.431Z","repository":{"id":57267866,"uuid":"61362796","full_name":"Deschtex/html2react","owner":"Deschtex","description":"A utility for turning raw HTML into React elements.","archived":false,"fork":false,"pushed_at":"2017-10-17T09:03:48.000Z","size":82,"stargazers_count":43,"open_issues_count":3,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-20T22:49:04.910Z","etag":null,"topics":["html","html2react","react"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Deschtex.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-06-17T09:35:12.000Z","updated_at":"2022-06-23T20:25:59.000Z","dependencies_parsed_at":"2022-09-02T02:50:09.702Z","dependency_job_id":null,"html_url":"https://github.com/Deschtex/html2react","commit_stats":null,"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Deschtex%2Fhtml2react","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Deschtex%2Fhtml2react/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Deschtex%2Fhtml2react/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Deschtex%2Fhtml2react/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Deschtex","download_url":"https://codeload.github.com/Deschtex/html2react/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254576300,"owners_count":22094332,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["html","html2react","react"],"created_at":"2024-11-19T23:12:59.953Z","updated_at":"2025-05-16T17:30:29.120Z","avatar_url":"https://github.com/Deschtex.png","language":"JavaScript","readme":"# HTML2React\n\n[![Build Status](https://img.shields.io/travis/Deschtex/html2react.svg?style=flat-square)](https://travis-ci.org/Deschtex/html2react)\n[![npm version](https://img.shields.io/npm/v/html2react.svg?style=flat-square)](https://www.npmjs.com/package/html2react)\n\nA utility for turning raw HTML into React elements.\n\n## Installation\n\n```\nnpm install --save html2react\n```\n\n## Usage\n\n### Basic HTML conversion\n\nIf you want to take raw HTML, SVG or any arbitrary XML and turn it into something that you can use in a React application, without using [dangerouslySetInnerHTML](https://facebook.github.io/react/tips/dangerously-set-inner-html.html), then you can simply pass it to `html2react`:\n\n```javascript\nimport React from 'react'\nimport { render } from 'react-dom'\nimport HTML2React from 'html2react'\n\nconst html = `\n  \u003ch1\u003eFoo\u003c/h1\u003e\n  \u003cp\u003e\u003ca href=\"#\" style=\"text-decoration: none;\"\u003eBar\u003c/a\u003e\u003c/p\u003e\n  \u003cp\u003eBaz\u003c/p\u003e\n`\n\nrender(\n  \u003cdiv\u003e\n    {HTML2React(html)}\n  \u003c/div\u003e,\n  document.getElementById('root')\n)\n```\n\n**Note:** All attributes but [event handlers](https://www.w3.org/TR/html5/webappapis.html#event-handlers-on-elements,-document-objects,-and-window-objects) will be transferred to the React elements.\n\n### HTML conversion with element overrides\n\nA powerful feature of `html2react` is the ability to target elements in the provided HTML and *override* them with [React components](https://facebook.github.io/react/docs/reusable-components.html), using nothing but [CSS selectors](https://www.w3.org/TR/css3-selectors/#selectors) for the mapping. Super simple!\n\nThe following example maps any `\u003ca\u003e` tag in the HTML to the local `Link` component:\n\n```javascript\nimport React from 'react'\nimport { render } from 'react-dom'\nimport HTML2React from 'html2react'\n\nfunction Link (props) {\n  return \u003ca {...props} style={{ textDecoration: 'none' }} /\u003e\n}\n\nconst html = `\n  \u003ch1\u003eFoo\u003c/h1\u003e\n  \u003cp\u003e\u003ca href=\"#\"\u003eBar\u003c/a\u003e\u003c/p\u003e\n  \u003cp\u003eBaz\u003c/p\u003e\n`\nconst content = HTML2React(html, {\n  a: Link\n})\n\nrender(\n  \u003cdiv\u003e\n    {content}\n  \u003c/div\u003e,\n  document.getElementById('root')\n)\n```\n\nThe following example maps any `\u003ca\u003e` tag with an `external` class to the local `ExternalLink` component. It also demonstrates a slightly more complex selector that maps only the third `\u003cp\u003e` tag to a `\u003cp\u003e` tag that wraps the local `Link` component:\n\n```javascript\nimport React from 'react'\nimport { render } from 'react-dom'\nimport HTML2React from 'html2react'\n\nfunction Link (props) {\n  return \u003ca {...props} style={{ textDecoration: 'none' }} /\u003e\n}\nfunction ExternalLink (props) {\n  return \u003cLink {...props} target='_blank' /\u003e\n}\n\nconst html = `\n  \u003ch1\u003eFoo\u003c/h1\u003e\n  \u003cp\u003e\u003ca href=\"http://bar\" class=\"external\"\u003eBar\u003c/a\u003e\u003c/p\u003e\n  \u003cp\u003e\u003ca href=\"#\"\u003eBaz\u003c/a\u003e\u003c/p\u003e\n  \u003cp\u003eQux\u003c/p\u003e\n`\nconst content = HTML2React(html, {\n  'a.external': ExternalLink,\n  'p:nth-of-type(3)': (props) =\u003e \u003cp\u003e\u003cLink {...props} /\u003e\u003c/p\u003e,\n  a: Link\n})\n\nrender(\n  \u003cdiv\u003e\n    {content}\n  \u003c/div\u003e,\n  document.getElementById('root')\n)\n```\n\n## License\n\nMIT (http://www.opensource.org/licenses/mit-license.php)\n\nSee [LICENSE](LICENSE) attached.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeschtex%2Fhtml2react","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeschtex%2Fhtml2react","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeschtex%2Fhtml2react/lists"}