Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC #30: React.forwardRef implementation #12346

Merged
merged 22 commits into from
Mar 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/react-reconciler/src/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,14 @@ export default function<T, P, I, TI, HI, PI, C, CC, CX, PL>(
}

function updateUseRef(current, workInProgress) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow why are these things so easy with Fiber

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know! 😁

const nextChildren = workInProgress.type.renderProp(
const renderProp = workInProgress.type.renderProp;
invariant(
typeof renderProp === 'function',
'useRef requires a render function but was given %s.%s',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message isn't great 😄 Any suggestions?

renderProp === null ? 'null' : typeof renderProp,
ReactDebugCurrentFiber.getCurrentFiberStackAddendum() || '',
);
const nextChildren = renderProp(
workInProgress.pendingProps,
workInProgress.ref,
);
Expand Down
31 changes: 30 additions & 1 deletion packages/react/src/__tests__/useRef-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ describe('useRef', () => {
let ReactFeatureFlags;
let ReactNoop;

function normalizeCodeLocInfo(str) {
return str && str.replace(/\(at .+?:\d+\)/g, '(at **)');
}

beforeEach(() => {
jest.resetModules();
ReactFeatureFlags = require('shared/ReactFeatureFlags');
Expand Down Expand Up @@ -159,7 +163,7 @@ describe('useRef', () => {
expect(ref.value).toBe(null);
});

it('should error if not provided a callback', () => {
it('should warn if not provided a callback during creation', () => {
expect(() => React.useRef(undefined)).toWarnDev(
'useRef requires a render function but was given undefined.',
);
Expand All @@ -170,4 +174,29 @@ describe('useRef', () => {
'useRef requires a render function but was given string.',
);
});

it('should error with a callstack if rendered without a function', () => {
let RefForwardingComponent;
expect(() => {
RefForwardingComponent = React.useRef();
}).toWarnDev('useRef requires a render function but was given undefined.');

ReactNoop.render(
<div>
<RefForwardingComponent />
</div>,
);

let caughtError;
try {
ReactNoop.flush();
} catch (error) {
caughtError = error;
}
expect(caughtError).toBeDefined();
expect(normalizeCodeLocInfo(caughtError.message)).toBe(
'useRef requires a render function but was given undefined.' +
(__DEV__ ? '\n in div (at **)' : ''),
);
});
});