-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Inconsistent call of onrender #177
Comments
|
I wonder how we can enforce
initStatements.push( deindent`
var mainFragment = renderMainFragment( state, this );
- if ( options.target ) this._mount( options.target );
+ if ( !options.root ) this._mount( options.target );
` ); That would cause an error when the compnent tried to append to a non-existent element, and it wouldn't add any code to components, but the error would be a bit mysterious.
I vote we start with 1 and then implement #13 soon, since there's lots of useful help we could give if we had a development mode. |
Well, option 1 doesn't work, because components can be mounted immediately (depending on whether they are top-level or not). So, option 3 it is. |
@Rich-Harris Don't quite get why 1) would not work. Can you elaborate on this? |
If you have a top-level component... <Widget/> ...it gets mounted after it's initially created: function renderMainFragment ( root, component ) {
var counter = new template.components.Widget({
target: null,
root: component.root || component,
data: counter_initialData
});
// ...
return {
mount: function ( target, anchor ) {
widget._mount( target, anchor );
}, But if it's inside an element... <div><Widget/></div> ...the component is appended to its containing element immediately, and the containing element gets appended later: function renderMainFragment ( root, component ) {
var div = document.createElement( 'div' );
var counter = new template.components.Widget({
target: div,
root: component.root || component,
data: counter_initialData
});
// ...
return {
mount: function ( target, anchor ) {
target.insertBefore( div, anchor );
}, So the presence of |
Yes, and if its a child component, the onrender hook will be bubbled up to the parent, which makes sure the callback in invoked after the component is mounted. Am I not really getting what you mean? |
@Swatinem Just looking at the source code it reads as if target can be optional from the public API point of view. Reading through the comments that is not the case. Making that somehow obvious would help those consuming (and eventually debugging) compiled components. |
throw in dev mode if options.target is absent
As of 1.9.0, if you compile in dev mode (with |
The generator allows
target
to be optional when instantiating a component (cf. https://github.com/sveltejs/svelte/blob/master/src/generate/index.js#L445).It will always call
onrender
though, whether or not the component was mounted or not (cf. https://github.com/sveltejs/svelte/blob/master/src/generate/index.js#L461).Expected Behavior
Example
For a component like this:
the generator will currently create the following code snippet:
The text was updated successfully, but these errors were encountered: