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

Warn when second argument to render is null and document.body is null as well #2448

Closed
gaearon opened this issue Nov 2, 2014 · 2 comments
Closed

Comments

@gaearon
Copy link
Collaborator

gaearon commented Nov 2, 2014

Too often I run into this:

 Invariant Violation: _registerComponent(...): Target container is not a DOM element.

when making a new project / example.

The reason is I'm passing document.body but React's script is in head and body doesn't exist yet.

This is very common problem as I observed for beginners, as they tend to put their scripts in head but React samples use document.body as mounting point.

The real problem here is the message: you wouldn't believe that document.body "is not a DOM element" and assume something else is wrong, without realizing it may be empty at the time.

I suggest we show a different, more specific warning when second argument is null and document.body is null. Something like "Target container is null. Are you trying to mount to document.body before it is ready?"

@syranide
Copy link
Contributor

syranide commented Nov 2, 2014

👍 I think many don't really understand what $(function() { ... }) does and do it just because their code didn't work otherwise, I like the "document.body warning"!

@sophiebits
Copy link
Collaborator

This sounds vaguely reasonable, but I think we're going to do #3211 instead so I'll close this.

josephsavona added a commit that referenced this issue May 15, 2024
See discussion on #2448 for full context. In the new 
`@enablePreserveExistingMemoizationGuarantees` mode, the goal is to preserve the 
existing referential equality guarantees from the original code. #2448 lays the 
groundwork by explicitly marking the _output_ of each useMemo block as memoized, 
hinting to the compiler that the value cannot subsequently change. This ensures 
the mutable range doesn't extend _later_, possibly overlapping a hook call and 
causing memoization to gett pruned. 

This PR fixes the other direction. There are cases where free variables 
referenced in the useMemo block could have been inferred as mutated, which could 
then extend the _start_ of the range earlier past a hook: 

```javascript 

const foo = createObject(); 

useBar(); 

const baz = useMemo(() => { 

const baz = createObject(); 

maybeMutate(foo, baz); 

return baz; 

}, [foo]); 

``` 

Here the compiler would infer that both `baz` and `foo` are mutable at the 
`maybeMutate()` call, grouping them in the same scope. But that scope would span 
the `useBar()` call, and be pruned, meaning that `baz` went unmemoized. 

However, useMemo blocks shouldn't be mutating free variables. Only variables 
newly created within the useMemo block should be mutable. So this PR extends the 
feature to treat all free variables referenced in a useMemo block as frozen as 
of the block itself.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants