In my Stencil component, I want to initiate my state variable with the passed down prop value. How to do that in Stencil?
I have tried assigning the value to the state var in componentWillLoad, but it doesn't work.
@Prop() passedVal
@State() someVal = ??
I am new to Stencil, and I come from VueJS, so please bear with my seemingly noobish question.
It's best to watch the prop for changes, then update the state.
@Component({ tag: 'my-comp' })
export class MyComp {
@Prop() foo: string;
@State() bar: string;
@Watch('foo')
onFooChange() {
this.bar = this.foo;
}
componentWillLoad() {
this.onFooChange();
}
render() {
return this.foo + this.bar;
}
}
You can call your watcher method in componentWillLoad
because watching will only start after the component has loaded.