61 | ```
62 |
63 | ## API
64 |
65 | ### reactElementToJSXString(ReactElement[, options])
66 |
67 | **options.displayName: function(ReactElement)**
68 |
69 | Provide a different algorithm in charge of finding the right display name (name of the underlying Class) for your element.
70 |
71 | Just return the name you want for the provided ReactElement, as a string.
72 |
73 | **options.filterProps: string[] | (val: any, key: string) => boolean, default []**
74 |
75 | If an array of strings is passed, filter out any prop who's name is in
76 | the array. For example ['key'] will suppress the key="" prop from being added.
77 |
78 | If a function is passed, it will be called for each prop with two arguments,
79 | the prop value and key, and will filter out any that return false.
80 |
81 | **options.showDefaultProps: boolean, default true**
82 |
83 | If true, default props shown.
84 |
85 | If false, default props are omitted unless they differ from from the default value.
86 |
87 | **options.showFunctions: boolean, default false**
88 |
89 | If true, functions bodies are shown.
90 |
91 | If false, functions bodies are replaced with `function noRefCheck() {}`.
92 |
93 | **options.functionValue: function, default `(fn) => fn`**
94 |
95 | Allows you to override the default formatting of function values.
96 |
97 | `functionValue` receives the original function reference as input
98 | and should send any value as output.
99 |
100 | **options.tabStop: number, default 2**
101 |
102 | Provide a different number of columns for indentation.
103 |
104 | **options.useBooleanShorthandSyntax: boolean, default true**
105 |
106 | If true, Boolean prop values will be omitted for [shorthand syntax](https://facebook.github.io/react/docs/jsx-in-depth.html#boolean-attributes).
107 |
108 | If false, Boolean prop values will be explicitly output like `prop={true}` and `prop={false}`
109 |
110 | **options.maxInlineAttributesLineLength: number, default undefined**
111 |
112 | Allows to render multiple attributes on the same line and control the behaviour.
113 |
114 | 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)
115 | 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`
116 | 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
117 | on multiple lines
118 |
119 | **options.sortProps: boolean, default true**
120 |
121 | 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.
122 |
123 | **options.useFragmentShortSyntax: boolean, default true**
124 |
125 | If true, fragment will be represented with the JSX short syntax `<>...>` (when possible).
126 |
127 | If false, fragment will always be represented with the JSX explicit syntax `...`.
128 |
129 | According to [the specs](https://reactjs.org/docs/fragments.html):
130 | - A keyed fragment will always use the explicit syntax: `...`
131 | - An empty fragment will always use the explicit syntax: ``
132 |
133 | Note: to use fragment you must use React >= 16.2
134 |
135 | ## Environment requirements
136 |
137 | The environment you use to use `react-element-to-jsx-string` should have [ES2015](https://babeljs.io/learn-es2015/) support.
138 |
139 | Use the [Babel polyfill](https://babeljs.io/docs/usage/polyfill/) or any other method that will make you
140 | environment behave like an ES2015 environment.
141 |
142 | ## Test
143 |
144 | ```sh
145 | yarn test
146 | yarn test:watch
147 | ```
148 |
149 | ## Build
150 |
151 | ```sh
152 | yarn build
153 | yarn build:watch
154 | ```
155 |
156 | ## Release
157 |
158 | Decide if this is a `patch`, `minor` or `major` release, look at http://semver.org/
159 |
160 | ```sh
161 | npm run release [major|minor|patch|x.x.x]
162 | ```
163 |
164 | ## Thanks
165 |
166 | [alexlande/react-to-jsx](https://github.com/alexlande/react-to-jsx/) was a good source of inspiration.
167 |
168 | We built our own module because we had some needs like ordering props in alphabetical order.
169 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | ## [17.0.1](https://github.com/algolia/react-element-to-jsx-string/compare/v17.0.0...v17.0.1) (2025-04-25)
2 |
3 |
4 |
5 | # [17.0.0](https://github.com/algolia/react-element-to-jsx-string/compare/v15.0.0...v17.0.0) (2025-01-18)
6 |
7 |
8 | ### Bug Fixes
9 |
10 | * **deps:** update dependency @base2/pretty-print-object to v1.0.2 ([3431228](https://github.com/algolia/react-element-to-jsx-string/commit/34312289d8b25d7d57c95c15a5b018a55d83546c))
11 |
12 |
13 | * React 19 (#865) ([e391aae](https://github.com/algolia/react-element-to-jsx-string/commit/e391aae7f0a113b44a7f55ca176cf93d6e6dfe11)), closes [#865](https://github.com/algolia/react-element-to-jsx-string/issues/865)
14 |
15 |
16 | ### BREAKING CHANGES
17 |
18 | * To simplify the library maintenance, this major version only support React 19.
19 |
20 |
21 |
22 | # [15.0.0](https://github.com/algolia/react-element-to-jsx-string/compare/v14.3.4...v15.0.0) (2022-05-09)
23 |
24 |
25 | ### Bug Fixes
26 |
27 | * add react 18 support, fixes [#728](https://github.com/algolia/react-element-to-jsx-string/issues/728) ([#729](https://github.com/algolia/react-element-to-jsx-string/issues/729)) ([8e17e12](https://github.com/algolia/react-element-to-jsx-string/commit/8e17e1283fd48108663c7fedf7d174c957c00f68))
28 |
29 |
30 |
31 | ## [14.3.4](https://github.com/algolia/react-element-to-jsx-string/compare/v14.3.3...v14.3.4) (2021-10-19)
32 |
33 |
34 | ### Bug Fixes
35 |
36 | * stringifying elements with props containing circular references ([#660](https://github.com/algolia/react-element-to-jsx-string/issues/660)) ([f203060](https://github.com/algolia/react-element-to-jsx-string/commit/f203060004e59af945019dad32a05f67508cc947))
37 |
38 |
39 |
40 | ## [14.3.3](https://github.com/algolia/react-element-to-jsx-string/compare/v14.3.2...v14.3.3) (2021-10-19)
41 |
42 |
43 | ### Bug Fixes
44 |
45 | * **deps:** pin dependency react-is to 17.0.2 ([a62c5fd](https://github.com/algolia/react-element-to-jsx-string/commit/a62c5fdaa33d4e891631ba48661f8466c96d809d))
46 | * fixed crashing on circular React elements ([#619](https://github.com/algolia/react-element-to-jsx-string/issues/619)) ([ea73118](https://github.com/algolia/react-element-to-jsx-string/commit/ea73118d80e668510d7de1ec23215248ef72b0a1))
47 | * handle invalid Date objects ([#605](https://github.com/algolia/react-element-to-jsx-string/issues/605)) ([#606](https://github.com/algolia/react-element-to-jsx-string/issues/606)) ([dbbd9e5](https://github.com/algolia/react-element-to-jsx-string/commit/dbbd9e5e0bb1e766910809da77e5a6126897202a))
48 |
49 |
50 | ### Features
51 |
52 | * support more element types ([#617](https://github.com/algolia/react-element-to-jsx-string/issues/617)) ([bf7f4cf](https://github.com/algolia/react-element-to-jsx-string/commit/bf7f4cf8d31494997b9d5f36f238286d67cd6ae1))
53 |
54 |
55 |
56 | ## [14.3.2](https://github.com/algolia/react-element-to-jsx-string/compare/v14.3.1...v14.3.2) (2020-10-28)
57 |
58 |
59 | ### Bug Fixes
60 |
61 | * **deps:** update dependency is-plain-object to v3.0.1 ([4974512](https://github.com/algolia/react-element-to-jsx-string/commit/4974512273c86c72415376fea89f9d6e07e2b2e5))
62 | * Handle multiple words before trailing space ([#572](https://github.com/algolia/react-element-to-jsx-string/issues/572)) ([e0c082e](https://github.com/algolia/react-element-to-jsx-string/commit/e0c082eb1d1e9fe2ed2918db157079d17af3af09))
63 |
64 |
65 |
66 | ## [14.3.1](https://github.com/algolia/react-element-to-jsx-string/compare/v14.3.0...v14.3.1) (2020-01-21)
67 |
68 |
69 | ### Bug Fixes
70 |
71 | * Fix TypeScript type declaration ([#516](https://github.com/algolia/react-element-to-jsx-string/issues/516)) ([c4fe54a](https://github.com/algolia/react-element-to-jsx-string/commit/c4fe54adcafbe688d397fe652e61952a56e7cffe))
72 |
73 |
74 |
75 | # [14.3.0](https://github.com/algolia/react-element-to-jsx-string/compare/v14.2.0...v14.3.0) (2020-01-17)
76 |
77 |
78 | ### Features
79 |
80 | * allow filterProps to be a function ([#417](https://github.com/algolia/react-element-to-jsx-string/issues/417)) ([c4908bb](https://github.com/algolia/react-element-to-jsx-string/commit/c4908bb))
81 |
82 |
83 |
84 | # [14.2.0](https://github.com/algolia/react-element-to-jsx-string/compare/v14.1.0...v14.2.0) (2019-12-29)
85 |
86 |
87 | ### Bug Fixes
88 |
89 | * **deps:** pin dependency @base2/pretty-print-object to 1.0.0 ([07f19f9](https://github.com/algolia/react-element-to-jsx-string/commit/07f19f95844681fc4a04e440138e41e385f98a75))
90 | * **formatting:** fix the anonymous functional component detection after babel upgrade ([7a1b93e](https://github.com/algolia/react-element-to-jsx-string/commit/7a1b93eca7446c4183298a03178f737678d85678))
91 |
92 |
93 | ### Features
94 |
95 | * **typescript:** Add TypeScript declaration file. ([#475](https://github.com/algolia/react-element-to-jsx-string/issues/475)) ([f9ea4e5](https://github.com/algolia/react-element-to-jsx-string/commit/f9ea4e54c3d01f45ace0038f2031bf9095a48138))
96 |
97 |
98 |
99 | # [14.1.0](https://github.com/algolia/react-element-to-jsx-string/compare/v14.0.3...v14.1.0) (2019-09-15)
100 |
101 |
102 | ### Bug Fixes
103 |
104 | * **deps:** Remove dependency stringify-object ([6dc6d8d](https://github.com/algolia/react-element-to-jsx-string/commit/6dc6d8d))
105 | * **deps:** Replace dependency stringify-object with pretty-print-object ([940a413](https://github.com/algolia/react-element-to-jsx-string/commit/940a413))
106 |
107 |
108 |
109 | ## [14.0.3](https://github.com/algolia/react-element-to-jsx-string/compare/v14.0.2...v14.0.3) (2019-07-19)
110 |
111 |
112 | ### Bug Fixes
113 |
114 | * **deps:** update dependency is-plain-object to v3 ([#361](https://github.com/algolia/react-element-to-jsx-string/issues/361)) ([b58cbbd](https://github.com/algolia/react-element-to-jsx-string/commit/b58cbbd))
115 | * Rework the propNameSorter to be less dependents of node sort internals ([a9ee312](https://github.com/algolia/react-element-to-jsx-string/commit/a9ee312))
116 | * **deps:** update dependency stringify-object to v3.3.0 ([bfe9a9f](https://github.com/algolia/react-element-to-jsx-string/commit/bfe9a9f))
117 | * **formatting:** Make the props "key" and "ref" order predictibale ([#340](https://github.com/algolia/react-element-to-jsx-string/issues/340)) ([3853463](https://github.com/algolia/react-element-to-jsx-string/commit/3853463))
118 |
119 |
120 | ### chore
121 |
122 | * **deps:** update jest monorepo to v23 (major) ([#305](https://github.com/algolia/react-element-to-jsx-string/issues/305)) ([aef55a2](https://github.com/algolia/react-element-to-jsx-string/commit/aef55a2))
123 |
124 |
125 | ### Features
126 |
127 | * **sortObject:** Add a test for issue 344 ([#357](https://github.com/algolia/react-element-to-jsx-string/issues/357)) ([5fe7604](https://github.com/algolia/react-element-to-jsx-string/commit/5fe7604)), closes [#334](https://github.com/algolia/react-element-to-jsx-string/issues/334)
128 |
129 |
130 | ### BREAKING CHANGES
131 |
132 | * **deps:** If you use the `showFunctions: true` option, the function are now always inlined in the output by default. Before it was not always the case (depending one the engine, platform or babel versions)
133 |
134 | You could get back to the previous behavior by using the `preserveFunctionLineBreak` function export as a value of the option `functionValue`.
135 |
136 | * test(smoke): Adapt the CommonJS bundle import
137 |
138 |
139 |
140 |
141 | ## [14.0.2](https://github.com/algolia/react-element-to-jsx-string/compare/v14.0.1...v14.0.2) (2018-10-10)
142 |
143 |
144 | ### Bug Fixes
145 |
146 | * **formatting:** Fix JSX element in prop object recursive loop ([#309](https://github.com/algolia/react-element-to-jsx-string/issues/309)) ([fd4f53b](https://github.com/algolia/react-element-to-jsx-string/commit/fd4f53b))
147 | * **functionValue:** handle nested datastructures ([94d1aeb](https://github.com/algolia/react-element-to-jsx-string/commit/94d1aeb))
148 |
149 |
150 |
151 |
152 | ## [14.0.1](https://github.com/algolia/react-element-to-jsx-string/compare/v14.0.0...v14.0.1) (2018-06-20)
153 |
154 |
155 | ### Bug Fixes
156 |
157 | * **browser:** Add IE 11 support ([#288](https://github.com/algolia/react-element-to-jsx-string/issues/288)) ([6c071b6](https://github.com/algolia/react-element-to-jsx-string/commit/6c071b6)), closes [#211](https://github.com/algolia/react-element-to-jsx-string/issues/211) [#285](https://github.com/algolia/react-element-to-jsx-string/issues/285)
158 | * **build:** missing babel helpers, true esm modules, simplify ([#290](https://github.com/algolia/react-element-to-jsx-string/issues/290)) ([faa8f46](https://github.com/algolia/react-element-to-jsx-string/commit/faa8f46))
159 |
160 |
161 |
162 |
163 | # [14.0.0](https://github.com/algolia/react-element-to-jsx-string/compare/v13.2.0...v14.0.0) (2018-05-25)
164 |
165 |
166 | ### Features
167 |
168 | * **browser:** build a dedicated version for the browser ([#242](https://github.com/algolia/react-element-to-jsx-string/issues/242)) ([574d850](https://github.com/algolia/react-element-to-jsx-string/commit/574d850))
169 |
170 |
171 | ### BREAKING CHANGES
172 |
173 | * **browser:** This PR change of the internal directory structure of the exported code. The previous code has move from the `dist/` into the `dist/esm` directory (but remender that we do not avice you to do use internals code 🤓)
174 |
175 | * fix(bunble): do not bundle peer dependencies
176 |
177 | * qa(ci): Avoid duplicate runs of checks on CI
178 |
179 | * qa(dependencies): Upgrade to latest rollup versions
180 |
181 | * qa(test): Allow to run the smoke tests aggaint all builded versions
182 |
183 |
184 |
185 |
186 | # [13.2.0](https://github.com/algolia/react-element-to-jsx-string/compare/v13.1.0...v13.2.0) (2018-03-14)
187 |
188 |
189 | ### Bug Fixes
190 |
191 | * **deps:** update dependency stringify-object to v3.2.2 ([b1a4c5e](https://github.com/algolia/react-element-to-jsx-string/commit/b1a4c5e))
192 |
193 |
194 |
195 |
196 | # [13.1.0](https://github.com/algolia/react-element-to-jsx-string/compare/v13.0.0...v13.1.0) (2017-11-15)
197 |
198 |
199 | ### Bug Fixes
200 |
201 | * **formatting:** Date/RegExp values output by formatComplexDataStructure ([#250](https://github.com/algolia/react-element-to-jsx-string/issues/250)) ([0387b72](https://github.com/algolia/react-element-to-jsx-string/commit/0387b72))
202 | * **react:** Fix warning about access to PropTypes using React 15.5+ (fixes [#213](https://github.com/algolia/react-element-to-jsx-string/issues/213)) ([2dcfd10](https://github.com/algolia/react-element-to-jsx-string/commit/2dcfd10))
203 | * **test:** Fix usage of yarn instead of npm in test script ([0db5aa4](https://github.com/algolia/react-element-to-jsx-string/commit/0db5aa4))
204 |
205 |
206 |
207 |
208 | # [13.0.0](https://github.com/algolia/react-element-to-jsx-string/compare/v12.0.0...v13.0.0) (2017-10-09)
209 |
210 |
211 | ### Bug Fixes
212 |
213 | * **deps:** update dependency stringify-object to v3.2.1 ([539ea56](https://github.com/algolia/react-element-to-jsx-string/commit/539ea56))
214 | * **formatting:** symbol description are now quoted ([2747f1b](https://github.com/algolia/react-element-to-jsx-string/commit/2747f1b)), closes [#134](https://github.com/algolia/react-element-to-jsx-string/issues/134)
215 | * **formatting:** trailing space ([2a07d5e](https://github.com/algolia/react-element-to-jsx-string/commit/2a07d5e)), closes [#135](https://github.com/algolia/react-element-to-jsx-string/issues/135)
216 |
217 |
218 | ### BREAKING CHANGES
219 |
220 | * **formatting:** Trailing are now preserved. In some rare case, `react-element-to-jsx-string` failed to respect the JSX specs for the trailing space. Event is the space were in the final output. There were silentrly ignored by JSX parser. This commit fix this bug by protecting the trailing space in the output.
221 |
222 | If we take the JSX:
223 | ```jsx
224 |
225 | foo bar baz
226 |
227 | ```
228 |
229 | Before it was converted to (the trailing space are replace by `*` for the readability):
230 | ```html
231 |
232 | foo*
233 |
234 | bar
235 |
236 | *baz
237 |
238 | ```
239 |
240 | Now there are preserved:
241 | ```html
242 |