Что я обнаружил, что работает, это добавление ссылки в компоненте реакции к освещенному элементу, а затем буквально установка свойства для него.
Итак, для следующего JSX:
<some-webcomponent ref={this.myRef}></some-webcomponent>
Вы можете передать свойство «some-webcomponent» в т. Е. ComponentDidMount:
componentDidMount () {
const element = this.myRef.current;
element.someCallback = () => // ...
}
Это не слишком красиво, но я бы тоже не считал это хаком. Требуется довольно много шаблонов, хотя: /
Вот полный компонент React для справки:
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
}
render() {
return <some-webcomponent ref={this.myRef}></some-webcomponent>;
}
componentDidMount() {
const element = this.myRef.current;
element.someCallback = () => console.log(“call!”);
}
}
Где горит элемент:
import { LitElement, html } from "lit-element";
class SomeWebcomponent extends LitElement {
static get properties() {
return {
someCallback: { type: Function }
};
}
render() {
return html`
<button @click=${this.someCallback}>click me</button>
`;
}
}
customElements.define("some-webcomponent", SomeWebcomponent);