Sitelet https://github.com/react/react/pull/9278
Skip to content

Allow returning null as host context - #9278

Merged
sophiebits merged 1 commit into
react:masterfrom
sophiebits:null-hc
Mar 30, 2017
Merged

sophiebits merged 1 commit into
react:masterfrom
sophiebits:null-hc

Conversation

@sophiebits

Copy link
Copy Markdown
Contributor

If your renderer doesn't use host context, you might prefer to return null. This used to give an error:

Invariant Violation: Expected host context to exist. This error is likely caused by a bug in React. Please file an issue.

I use a sentinel value instead now.

The code in ReactFiberHostContext is a little complicated now. We could probably also just remove the invariants.

@gaearon

gaearon commented Mar 29, 2017

Copy link
Copy Markdown
Contributor

Circle failed

@sophiebits

Copy link
Copy Markdown
Contributor Author

I saw. Fixed now. :)

'Expected host context to exist. This error is likely caused by a bug ' +
'in React. Please file an issue.',
);
return (c: any);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do you need this cast?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Proof it isn't NoContextT.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Flow doesn't know NO_CONTEXT is the only possible inhabitant.

var React;
var ReactFiberReconciler;

describe('ReactCoroutine', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bad describe

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

ReactFiberReconciler = require('ReactFiberReconciler');
});

it('works with null host context', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Instead of a separate test for an internal, maybe we can change ReactNoop to return null instead? It's not using the context anyway.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is public-ish renderer API. I'd rather have a test just for this so we don't change it later accidentally.

? contextStackCursor.current
: emptyObject;
const rootInstance = requiredContext(rootInstanceStackCursor.current);
const context = requiredContext(contextStackCursor.current);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This didn't use to be required. Does it matter? I don't remember why I didn't check it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It used to be emptyObject if missing which seems wrong.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

At the very least, it was inconsistent with our declared type signatures for the host config (but emptyObject isn't typed so).

If your renderer doesn't use host context, you might prefer to return null. This used to give an error:

> Invariant Violation: Expected host context to exist. This error is likely caused by a bug in React. Please file an issue.

I use a sentinel value instead now.

The code in ReactFiberHostContext is a little complicated now. We could probably also just remove the invariants.
@sophiebits
sophiebits merged commit e57ad7f into react:master Mar 30, 2017
mrizwanashiq pushed a commit to mrizwanashiq/react that referenced this pull request Jun 25, 2026
If your renderer doesn't use host context, you might prefer to return null. This used to give an error:

> Invariant Violation: Expected host context to exist. This error is likely caused by a bug in React. Please file an issue.

I use a sentinel value instead now.

The code in ReactFiberHostContext is a little complicated now. We could probably also just remove the invariants.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants