1.0.2 • Published 3 years ago

react-replace-html v1.0.2

Weekly downloads
1
License
MIT
Repository
-
Last release
3 years ago

react-replace-html

Replace HTML with react components

Build Status npm package Coverage Status

Installation

npm install --save react-replace-html

Usage

import parse from "react-replace-html";

function RedComponent(props) {
  return (
    <span style={{ color: "red", fontStyle: "italic" }}>
      {props.children} 🍕
    </span>
  );
}

const html =
  '<h3>Title</h3><p>Lorem <span class="red">ipsum</span> <span class="blue">dolor</span> sit amet</p>';

const replacer = (tag, props) => {
  if (tag === "span" && props.class === "red") {
    return <RedComponent {...props} />;
  }

  return React.createElement(tag, props);
};

parse(html, replacer);

Replacer function

replacer(tag, props)

const replacer = (tag, props) => {
  if (tag === "span" && props.class === "red") {
    return <RedComponent {...props} />;
  }

  return React.createElement(tag, props);
};

License

MIT

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago