如图所示,使用antd写的组件,使用shadow Dom隔离后样式不显示了,请问有人知道什么原因吗?
import React from "react";import ReactDOM from "react-dom";
export function ShadowContent({ root, children }) {
return ReactDOM.createPortal(children, root);}
export class ShadowView extends React.Component {
state = { root: null };
setRoot = eleemnt => {
const root = eleemnt.attachShadow({ mode: "open" });
this.setState({ root });
};
render() {
const { children } = this.props;
const { root } = this.state;
return <div ref={this.setRoot}>
{root && <ShadowContent root={root} >
{children}
</ShadowContent>}
</div>;
}}
export class App extends React.Component {
state = { message: '...' };
onBtnClick = () => {
this.setState({ message: 'haha' });
}
render() {
const { message } = this.state;
return <ShadowView>
<div>{message}</div>
<button onClick={this.onBtnClick}>单击我</button>
</ShadowView>
}}
ReactDOM.render(<App />, document.getElementById("root"));
样式隔离
// https://github.com/styled-components/styled-components/pull/1491
<StyleSheetManager target={target.shadowRoot}>
<React.Fragment>
{/* your children here */}
</React.Fragment>
</StyleSheetManager>
使用 ReactDOM.createPortal 我还解决了这个问题:?? 如何优雅地解决多个 React、Vue App 之间的状态共享?
——————————————————————————————————————————————————
如果能够对你有帮助就请「点赞」「采纳」吧,感谢 ??