diff --git a/README.md b/README.md
index fca60b0cc..1b859440f 100644
--- a/README.md
+++ b/README.md
@@ -86,6 +86,13 @@ console.log(reactElementToJSXString(
'value'}/>, {showFunctions: true, functionValue: () => '...'})
+ ).toEqual('
');
+ });
+ it('sends the original fn to functionValue', () => {
+ const fn = () => {};
+ const functionValue = receivedFn => expect(receivedFn).toBe(fn);
+ reactElementToJSXString(
, {functionValue});
+ });
+ it('should return noRefCheck when "showFunctions" is false and "functionValue" is not provided', () => {
+ expect(
+ reactElementToJSXString(
{}}/>)
+ ).toEqual('
');
+ });
});
diff --git a/index.js b/index.js
index 60e0b2b2c..188ddb0e6 100644
--- a/index.js
+++ b/index.js
@@ -6,12 +6,15 @@ import sortobject from 'sortobject';
import traverse from 'traverse';
import {fill} from 'lodash';
+const defaultFunctionValue = fn => fn;
+
export default function reactElementToJSXString(
ReactElement, {
displayName,
filterProps = [],
showDefaultProps = true,
showFunctions = false,
+ functionValue = defaultFunctionValue,
tabStop = 2,
useBooleanShorthandSyntax = true,
maxInlineAttributesLineLength,
@@ -189,12 +192,21 @@ got \`${typeof Element}\``
return typeof value;
}
+ function isFunction (value) {
+ return typeof value === 'function';
+ }
+
function formatValue(value, inline, lvl) {
const wrapper = '__reactElementToJSXString__Wrapper__';
+ if (isFunction(value)) {
+ return functionValue(
+ showFunctions === false && functionValue === defaultFunctionValue ?
+ function noRefCheck() {} : // eslint-disable-line prefer-arrow-callback
+ value
+ );
+ }
- if (typeof value === 'function' && !showFunctions) {
- return function noRefCheck() {};
- } else if (isElement(value)) {
+ if (isElement(value)) {
// we use this delimiter hack in cases where the react element is a property
// of an object from a root prop
// i.e.