react-element-to-jsx-string
Turn a ReactElement into the corresponding JSX string.
Last updated 3 years ago by spy-seth .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install react-element-to-jsx-string 
SYNC missed versions from official npm registry.

react-element-to-jsx-string

Version Build Status License Downloads

Turn a ReactElement into the corresponding JSX string.

Useful for unit testing and any other need you may think of.

Features:

  • supports nesting and deep nesting like <div a={{b: {c: {d: <div />}}}} />
  • props: supports string, number, function (inlined as prop={function noRefCheck() {}}), object, ReactElement (inlined), regex, booleans (with or without shorthand syntax), ...
  • order props alphabetically
  • sort object keys in a deterministic order (o={{a: 1, b:2}} === o={{b:2, a:1}})
  • handle ref and key attributes, they are always on top of props
  • React's documentation indent style for JSX

Table of Contents generated with DocToc

Setup

yarn add react-element-to-jsx-string [--dev]

Usage

import React from 'react';
import reactElementToJSXString from 'react-element-to-jsx-string';

console.log(reactElementToJSXString(<div a="1" b="2">Hello, world!</div>));
// <div
//   a="1"
//   b="2"
// >
//   Hello, world!
// </div>

API

reactElementToJSXString(ReactElement[, options])

options.displayName: function(ReactElement)

Provide a different algorithm in charge of finding the right display name (name of the underlying Class) for your element.

Just return the name you want for the provided ReactElement, as a string.

options.filterProps: string[] | (val: any, key: string) => boolean, default []

If an array of strings is passed, filter out any prop who's name is in the array. For example ['key'] will suppress the key="" prop from being added.

If a function is passed, it will be called for each prop with two arguments, the prop value and key, and will filter out any that return false.

options.showDefaultProps: boolean, default true

If true, default props shown.

If false, default props are omitted unless they differ from from the default value.

options.showFunctions: boolean, default false

If true, functions bodies are shown.

If false, functions bodies are replaced with function noRefCheck() {}.

options.functionValue: function, default (fn) => fn

Allows you to override the default formatting of function values.

functionValue receives the original function reference as input and should send any value as output.

options.tabStop: number, default 2

Provide a different number of columns for indentation.

options.useBooleanShorthandSyntax: boolean, default true

If true, Boolean prop values will be omitted for shorthand syntax.

If false, Boolean prop values will be explicitly output like prop={true} and prop={false}

options.maxInlineAttributesLineLength: number, default undefined

Allows to render multiple attributes on the same line and control the behaviour.

You can provide the max number of characters to render inline with the tag name. If the number of characters on the line (including spacing and the tag name) exceeds this number, then all attributes will be rendered on a separate line. The default value of this option is undefined. If this option is undefined then if there is more than one attribute on an element, they will render on their own line. Note: Objects passed as attribute values are always rendered on multiple lines

options.sortProps: boolean, default true

Either to sort or not props. If you use this lib to make some isomorphic rendering you should set it to false, otherwise this would lead to react invalid checksums as the prop order is part of react isomorphic checksum algorithm.

options.useFragmentShortSyntax: boolean, default true

If true, fragment will be represented with the JSX short syntax <>...</> (when possible).

If false, fragment will always be represented with the JSX explicit syntax <React.Fragment>...</React.Fragment>.

According to the specs:

  • A keyed fragment will always use the explicit syntax: <React.Fragment key={...}>...</React.Fragment>
  • An empty fragment will always use the explicit syntax: <React.Fragment />

Note: to use fragment you must use React >= 16.2

Environment requirements

The environment you use to use react-element-to-jsx-string should have ES2015 support.

Use the Babel polyfill or any other method that will make you environment behave like an ES2015 environment.

Test

yarn test
yarn test:watch

Build

yarn build
yarn build:watch

Release

Decide if this is a patch, minor or major release, look at http://semver.org/

npm run release [major|minor|patch|x.x.x]

Thanks

alexlande/react-to-jsx was a good source of inspiration.

We built our own module because we had some needs like ordering props in alphabetical order.

Current Tags

  • 15.0.0                                ...           latest (3 years ago)

66 Versions

  • 15.0.0                                ...           3 years ago
  • 14.3.4                                ...           3 years ago
  • 14.3.3                                ...           3 years ago
  • 14.3.2                                ...           4 years ago
  • 14.3.1                                ...           5 years ago
  • 14.3.0                                ...           5 years ago
  • 14.2.0                                ...           5 years ago
  • 14.1.0                                ...           5 years ago
  • 14.0.3                                ...           5 years ago
  • 14.0.2                                ...           6 years ago
  • 14.0.1                                ...           6 years ago
  • 14.0.0                                ...           7 years ago
  • 13.2.0                                ...           7 years ago
  • 13.1.0                                ...           7 years ago
  • 13.0.0                                ...           7 years ago
  • 12.0.0                                ...           7 years ago
  • 11.0.1                                ...           7 years ago
  • 11.0.0                                ...           7 years ago
  • 10.1.0                                ...           7 years ago
  • 10.0.1                                ...           7 years ago
  • 10.0.0                                ...           7 years ago
  • 8.0.0                                ...           8 years ago
  • 7.0.0                                ...           8 years ago
  • 6.4.0 [deprecated]           ...           8 years ago
  • 6.3.0                                ...           8 years ago
  • 6.2.0                                ...           8 years ago
  • 6.0.0                                ...           8 years ago
  • 5.0.7                                ...           8 years ago
  • 5.0.6                                ...           8 years ago
  • 5.0.5                                ...           8 years ago
  • 5.0.4                                ...           8 years ago
  • 5.0.3                                ...           8 years ago
  • 5.0.2                                ...           8 years ago
  • 5.0.1                                ...           8 years ago
  • 5.0.0                                ...           8 years ago
  • 4.2.1                                ...           8 years ago
  • 4.2.0                                ...           8 years ago
  • 4.1.0                                ...           8 years ago
  • 4.0.0                                ...           8 years ago
  • 3.2.0                                ...           8 years ago
  • 3.1.2                                ...           8 years ago
  • 3.1.1                                ...           8 years ago
  • 3.1.0                                ...           8 years ago
  • 3.0.0                                ...           9 years ago
  • 2.6.1                                ...           9 years ago
  • 2.6.0                                ...           9 years ago
  • 2.5.0                                ...           9 years ago
  • 2.4.0                                ...           9 years ago
  • 2.3.0                                ...           9 years ago
  • 2.2.0                                ...           9 years ago
  • 2.1.5                                ...           9 years ago
  • 2.1.4                                ...           9 years ago
  • 2.1.3                                ...           9 years ago
  • 2.1.2                                ...           9 years ago
  • 2.1.1                                ...           9 years ago
  • 2.1.0                                ...           9 years ago
  • 2.0.5                                ...           9 years ago
  • 2.0.4                                ...           9 years ago
  • 2.0.3                                ...           9 years ago
  • 2.0.2                                ...           9 years ago
  • 2.0.1                                ...           9 years ago
  • 2.0.0                                ...           9 years ago
  • 1.1.2                                ...           9 years ago
  • 1.1.1                                ...           9 years ago
  • 1.1.0                                ...           9 years ago
  • 1.0.0                                ...           9 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |