Skip to content

Commit 66e8307

Browse files
shouzevvo
authored andcommitted
feat(sortProps): add option to remove sorting of props
1 parent 4072a81 commit 66e8307

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ console.log(reactElementToJSXString(<div a="1" b="2">Hello, world!</div>));
112112
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
113113
on multiple lines
114114

115+
**options.sortProps: boolean, default true**
116+
117+
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.
118+
115119
## Environment requirements
116120

117121
The environment you use to use `react-element-to-jsx-string` should have [ES2015](https://babeljs.io/learn-es2015/) support.

index-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,19 @@ describe('reactElementToJSXString(ReactElement)', () => {
203203
);
204204
});
205205

206+
it('reactElementToJSXString(<div z="3" a="1" b="2"/>, {sortProps: false})', () => {
207+
/* eslint react/jsx-sort-props: 0 */
208+
expect(
209+
reactElementToJSXString(<div z="3" a="1" b="2" />, { sortProps: false })
210+
).toEqual(
211+
`<div
212+
z="3"
213+
a="1"
214+
b="2"
215+
/>`
216+
);
217+
});
218+
206219
it('reactElementToJSXString(<div a="1">Hello</div>)', () => {
207220
expect(reactElementToJSXString(<div a="1">Hello</div>)).toEqual(
208221
`<div a="1">

index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default function reactElementToJSXString(
2020
tabStop = 2,
2121
useBooleanShorthandSyntax = true,
2222
maxInlineAttributesLineLength,
23+
sortProps = true,
2324
} = {}
2425
) {
2526
const getDisplayName = displayName || getDefaultDisplayName;
@@ -47,6 +48,7 @@ got \`${typeof Element}\``
4748
Element.props,
4849
getDefaultProps(Element),
4950
inline,
51+
sortProps,
5052
lvl
5153
);
5254
let attributes = [];
@@ -170,7 +172,7 @@ got \`${typeof Element}\``
170172
}
171173
}
172174

173-
function formatProps(props, defaultProps, inline, lvl) {
175+
function formatProps(props, defaultProps, inline, sortProps, lvl) {
174176
let formatted = Object.keys(props).filter(noChildren);
175177

176178
if (useBooleanShorthandSyntax) {
@@ -188,9 +190,13 @@ got \`${typeof Element}\``
188190
);
189191
}
190192

191-
return formatted
192-
.sort()
193-
.map(propName => getJSXAttribute(propName, props[propName], inline, lvl));
193+
if (sortProps) {
194+
formatted = formatted.sort();
195+
}
196+
197+
return formatted.map(propName =>
198+
getJSXAttribute(propName, props[propName], inline, lvl)
199+
);
194200
}
195201

196202
function getJSXAttribute(name, value, inline, lvl) {

0 commit comments

Comments
 (0)