Giter VIP home page Giter VIP logo

babel-plugin-react-element-to-jsx-string's Introduction

babel-plugin-react-element-to-jsx-string

Turn a ReactElement into the corresponding JSX string at build time.

Table of Contents

Example

Mark your React Component with the comment /* react-element-to-jsx-string */:

import React from 'react';

/* react-element-to-jsx-string */
const Article = ({ title, content }) => (
  <article>
    <h1>{title}</h1>
    <p>{content}</p>
  </article>
);

export default Article;

And the JSX as string will be added next to your React Component:

import React from 'react';

/* react-element-to-jsx-string */
const Article = ({ title, content }) => (
  <article>
    <h1>{title}</h1>
    <p>{content}</p>
  </article>
);
Article.jsxString = "<article>\n  <h1>{title}</h1>\n  <p>{content}</p>\n</article>";

export default Article;

Install

Node.js v8 or newer is required.

Via the yarn client:

$ yarn add --dev babel-plugin-react-element-to-jsx-string

Via the npm client:

$ npm install --save-dev babel-plugin-react-element-to-jsx-string

Usage

  1. Add to your babel config this plugin:
{
  "plugins": ["babel-plugin-react-element-to-jsx-string"]
}
  1. Add before a React Component a comment with /* react-element-to-jsx-string */, and the first JSX code will be added as string in a property called jsxString.

  2. What about changing the default property name? That can be done in two ways:

  • Via the flag comment:
/* react-element-to-jsx-string: fooBar */
  • Via the plugin options:
{
  "plugins": [["babel-plugin-react-element-to-jsx-string", { "id": "fooBar" }]]
}

Contribute

Feel free to dive in! Open an issue or submit PRs.

babel-plugin-react-element-to-jsx-string follows the Contributor Covenant Code of Conduct.

License

MIT

babel-plugin-react-element-to-jsx-string's People

Contributors

ramasilveyra avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.