Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rehype-react

Package Overview
Dependencies
Maintainers
2
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rehype-react

rehype plugin to transform to React

  • 8.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
118K
increased by5.35%
Maintainers
2
Weekly downloads
 
Created

What is rehype-react?

rehype-react is a plugin for the rehype ecosystem that allows you to transform HTML into React components. It is particularly useful for rendering HTML content in React applications, enabling you to leverage React's component-based architecture while working with HTML content.

What are rehype-react's main functionalities?

Transform HTML to React Components

This feature allows you to transform HTML strings into React components. The code sample demonstrates how to use rehype-react with unified and rehype-parse to parse an HTML string and convert it into a React element that can be rendered using ReactDOM.

const rehypeReact = require('rehype-react');
const unified = require('unified');
const rehypeParse = require('rehype-parse');
const React = require('react');
const ReactDOM = require('react-dom');

const processor = unified()
  .use(rehypeParse, { fragment: true })
  .use(rehypeReact, { createElement: React.createElement });

const html = '<div>Hello, <strong>world</strong>!</div>';
const reactElement = processor.processSync(html).result;

ReactDOM.render(reactElement, document.getElementById('root'));

Custom Component Mapping

This feature allows you to map HTML tags to custom React components. The code sample shows how to replace the default 'div' tag with a custom React component 'MyComponent' that styles the text in red.

const rehypeReact = require('rehype-react');
const unified = require('unified');
const rehypeParse = require('rehype-parse');
const React = require('react');
const ReactDOM = require('react-dom');

const MyComponent = (props) => <div style={{ color: 'red' }}>{props.children}</div>;

const processor = unified()
  .use(rehypeParse, { fragment: true })
  .use(rehypeReact, { createElement: React.createElement, components: { div: MyComponent } });

const html = '<div>Hello, <strong>world</strong>!</div>';
const reactElement = processor.processSync(html).result;

ReactDOM.render(reactElement, document.getElementById('root'));

Other packages similar to rehype-react

Keywords

FAQs

Package last updated on 01 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc