这可能有点棘手 - 我想在通过 Stencil 准备的 Web 组件中渲染 React,但我得到“不变违规:目标容器不是 DOM 元素。”:
import { Component, Prop } from "@stencil/core";
import { format } from "../../utils/utils";
import ReactDOM from 'react-dom';
import React from "react";
import { LikeButton } from './../LikeButton';
const e = React.createElement;
@Component({
tag: "my-component",
styleUrl: "my-component.css",
shadow: true
})
export class MyComponent {
@Prop() first: string;
@Prop() middle: string;
@Prop() last: string;
private getText(): string {
return format(this.first, this.middle, this.last);
}
componentWillLoad() {
console.log("here i am - the React render");
const domContainer = document.querySelector("#like_button_container");
ReactDOM.render(e(LikeButton), domContainer);
}
render() {
return (
<div>
Hello, World! I'm {this.getText()}{" "}
<div id="like_button_container">React container</div>
</div>
);
}
}