Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Turn a ReactElement into the corresponding JSX string

License

NotificationsYou must be signed in to change notification settings

algolia/react-element-to-jsx-string

Repository files navigation

VersionBuild StatusLicenseDownloads

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 asprop={function noRefCheck() {}}), object, ReactElement (inlined), regex, booleans (with or withoutshorthand syntax), ...
  • order props alphabetically
  • sort object keys in a deterministic order (o={{a: 1, b:2}} === o={{b:2, a:1}})
  • handleref andkey attributes, they are always on top of props
  • React's documentation indent style for JSX

Table of Contentsgenerated withDocToc

Setup

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

Usage

importReactfrom'react';importreactElementToJSXStringfrom'react-element-to-jsx-string';console.log(reactElementToJSXString(<diva="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 inthe 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 withfunction noRefCheck() {}.

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

Allows you to override the default formatting of function values.

functionValue receives the original function reference as inputand 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 forshorthand syntax.

If false, Boolean prop values will be explicitly output likeprop={true} andprop={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 isundefined. If this option isundefinedthen 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 renderedon 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 tothe 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 usereact-element-to-jsx-string should haveES2015 support.

Use theBabel polyfill or any other method that will make youenvironment behave like an ES2015 environment.

Test

yarntestyarn test:watch

Build

yarn buildyarn build:watch

Release

Decide if this is apatch,minor ormajor release, look athttp://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.

About

Turn a ReactElement into the corresponding JSX string

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors43


[8]ページ先頭

©2009-2025 Movatter.jp