A button toggles a boolean value in MainComp
.
I want to propagate the value in both the nested child components. My real project works with the first level child, it fails to update the second level child component.
But oddly enough trying to replicate the code in codepen I noticed it doesn't work for the first level child either.
https://codepen.io/alfredopacino/pen/eqVGJa?editors=1001
class MainComp extends LitElement {
constructor() {
super()
this.booleanValue = true
}
toggleBooleanValue() {
this.booleanValue = !this.booleanValue
this.requestUpdate();
}
render() {
return html `
<style> div {padding: 1em; border:1px solid #000} </style>
<div> main component booleanValue: ${!!this.booleanValue} <button @click=${this.toggleBooleanValue}>toggle</button>
<child-comp ?booleanValue=${this.booleanValue}></child-comp>
</div>
`
}
}
class ChildComp extends LitElement {
constructor() {
super()
}
static get properties() {
return {
booleanValue: Boolean
}
}
render() {
return html `
<style> div {padding: 1em; border:1px solid green} </style>
<div> child component booleanValue: ${!!this.booleanValue}
<child-2-comp ?booleanValue=${this.booleanValue}></child-2-comp>
</div>
`
}
}
class Child_2_Comp extends LitElement {
constructor() {
super()
}
static get properties() {
return {
booleanValue: Boolean
}
}
render() {
return html `
<style> div {padding: 1em; border:1px solid red} </style>
<div> child 2 component booleanValue: ${!!this.booleanValue}</div>
`
}
}