Skip to content

Commit e83ec6d

Browse files
skiritsisyenshih
authored andcommitted
Updated misleading error message in production environment when adding ref to a functional component (facebook#11761) (facebook#11782)
* Updated misleading error message in production environment when adding ref to a functional component * Reverted changes to codes.json * Updated error message
1 parent 0441e5f commit e83ec6d

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

packages/react-dom/src/__tests__/ReactStatelessComponent-test.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,14 @@ describe('ReactStatelessComponent', () => {
144144
}).toThrowError(
145145
__DEV__
146146
? 'Stateless function components cannot have refs.'
147-
: // TODO: the different message in production seems like a bug.
148-
// It happens because we don't save _owner in production for
149-
// functional components. We should probably show a better message.
150-
'Element ref was specified as a string (me) but no owner was set.',
147+
: // It happens because we don't save _owner in production for
148+
// functional components.
149+
'Element ref was specified as a string (me) but no owner was set. This could happen for one of' +
150+
' the following reasons:\n' +
151+
'1. You may be adding a ref to a functional component\n' +
152+
"2. You may be adding a ref to a component that was not created inside a component's render method\n" +
153+
'3. You have multiple copies of React loaded\n' +
154+
'See https://fb.me/react-refs-must-have-owner for more information.',
151155
);
152156
});
153157

packages/react-dom/src/__tests__/multiple-copies-of-react-test.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ describe('when different React version is used with string ref', () => {
2525
expect(() => {
2626
ReactTestUtils.renderIntoDocument(<TextWithStringRef />);
2727
}).toThrow(
28-
'Element ref was specified as a string (foo) but no owner was set.' +
29-
' You may have multiple copies of React loaded. (details: ' +
30-
'https://fb.me/react-refs-must-have-owner).',
28+
'Element ref was specified as a string (foo) but no owner was set. This could happen for one of' +
29+
' the following reasons:\n' +
30+
'1. You may be adding a ref to a functional component\n' +
31+
"2. You may be adding a ref to a component that was not created inside a component's render method\n" +
32+
'3. You have multiple copies of React loaded\n' +
33+
'See https://fb.me/react-refs-must-have-owner for more information.',
3134
);
3235
});
3336
});

packages/react-dom/src/__tests__/refs-test.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,12 @@ describe('creating element with ref in constructor', () => {
392392
expect(function() {
393393
ReactTestUtils.renderIntoDocument(<RefTest />);
394394
}).toThrowError(
395-
'Element ref was specified as a string (p) but no owner was ' +
396-
'set. You may have multiple copies of React loaded. ' +
397-
'(details: https://fb.me/react-refs-must-have-owner).',
395+
'Element ref was specified as a string (p) but no owner was set. This could happen for one of' +
396+
' the following reasons:\n' +
397+
'1. You may be adding a ref to a functional component\n' +
398+
"2. You may be adding a ref to a component that was not created inside a component's render method\n" +
399+
'3. You have multiple copies of React loaded\n' +
400+
'See https://fb.me/react-refs-must-have-owner for more information.',
398401
);
399402
});
400403
});

packages/react-reconciler/src/ReactChildFiber.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,12 @@ function coerceRef(current: Fiber | null, element: ReactElement) {
138138
);
139139
invariant(
140140
element._owner,
141-
'Element ref was specified as a string (%s) but no owner was ' +
142-
'set. You may have multiple copies of React loaded. ' +
143-
'(details: https://fb.me/react-refs-must-have-owner).',
141+
'Element ref was specified as a string (%s) but no owner was set. This could happen for one of' +
142+
' the following reasons:\n' +
143+
'1. You may be adding a ref to a functional component\n' +
144+
"2. You may be adding a ref to a component that was not created inside a component's render method\n" +
145+
'3. You have multiple copies of React loaded\n' +
146+
'See https://fb.me/react-refs-must-have-owner for more information.',
144147
mixedRef,
145148
);
146149
}

0 commit comments

Comments
 (0)