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

Pass options through to oncreate #550

Closed
Rich-Harris opened this issue May 2, 2017 · 5 comments · Fixed by #794
Closed

Pass options through to oncreate #550

Rich-Harris opened this issue May 2, 2017 · 5 comments · Fixed by #794

Comments

@Rich-Harris
Copy link
Member

This has come up (most recently) once or twice in discussion — there are some situations where it would be helpful to be able to access the instantiation options in oncreate (though I don't think it's something we should encourage, it's a useful escape hatch).

@tomcon
Copy link

tomcon commented May 4, 2017

I admire the simplicity and power of svelte but did wonder about the lack of lifecycle events. Unless I'm misunderstanding this wouldn't a beforeCreate event to go with onCreate/afterCreate event be simpler? (likewise for destroy). Am assuming target node would be accessible in the updated onCreate/afterCreate.

@Rich-Harris
Copy link
Member Author

What would you do in a beforeCreate that you wouldn't do in oncreate? (Genuine question.)

@tomcon
Copy link

tomcon commented May 4, 2017

Had thought it might be the place to grab some data or prep work but it does seem to be redundant. Coming from Vue, had thought that with mounted and other LC events that additional svelte events may be needed. Ignore this please Rich and it's refreshing to keep things minimal. Don't think beforeCreate is used much in Vue after all!

@m59peacemaker
Copy link
Contributor

m59peacemaker commented Aug 28, 2017

./Nested.html here is an example of something I am continually desiring, accomplished with hackery
https://svelte.technology/repl?version=1.30.0&gist=77ab59c28b56d44f6fb25cb750af9a20

And here's a less magical, but still hack-ugly version that rolls its own on:create https://svelte.technology/repl?version=1.30.0&gist=45b4a22ac71d357492994af0efc6837e

Can we have this?

<Counter ref:counter/>
<Component arguments="{ counter: this.refs.counter }"/>
<!-- Component.html -->
<button on:click="counter.increment()">increment</button>

<script>
export default {
  oncreate ({ counter }) {
    this.counter = counter
  }
}

@Rich-Harris
Copy link
Member Author

As of 1.34, you can access this.options inside lifecycle hooks and methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants