gpt4 book ai didi

javascript - 是否有可能将 react 上下文注入(inject) react 中根外的div(在 body 下方)

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:39:45 25 4
gpt4 key购买 nike

我正在使用 React cytoscape 库。尝试集成到 cytoscape 的 popper 插件。popper 的“content”属性期望返回一个 div 元素,描述附加到“body”元素的 popper。由于上下文提供者位于根元素下,因此该 div 不能成为该上下文的消费者。我如何在定义在根之外的 popper 元素中也使用相同的上下文。

使用 cytoscape 的 react 组件,但 cytoscape 的插件是纯 js。

<body>
<div id=root>
<Style_Provider>
.
.
.
</Style_Provider>
</div>
<div id="popper">
// need to access to style context
</div>
</body>

最佳答案

作为Eric Hasselbring said , portals解决这个用例:

Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.

这是一个使用上下文的例子:

const ExampleContext = React.createContext(
"default context"
);

class Example extends React.Component {
render() {
return (
<ExampleContext.Provider value="context from Example">
<div>
This is the parent comnponent.
<MyPortal />
</div>
</ExampleContext.Provider>
);
}
}

class MyPortal extends React.Component {
static contextType = ExampleContext;
render() {
return ReactDOM.createPortal(
<div>This is "my portal," context stuff is "{this.context}"</div>,
document.getElementById("portal")
);
}
}

ReactDOM.render(
<Example />,
document.getElementById("root")
);
<div id="root"></div>
<hr>
<div id="portal"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>


在评论中你说:

the popper creates those 'divs' dynamically. Hence, I cant create 'MyPortal' component initially.

只需要有条件地创建门户即可。这是上面的内容,但带有一个按钮来显示/隐藏门户:

const ExampleContext = React.createContext(
"default context"
);

class Example extends React.Component {
constructor(props) {
super(props);
this.state = {
showPortal: false
};
this.showHideClick = this.showHideClick.bind(this);
}

showHideClick() {
this.setState(({showPortal}) => ({showPortal: !showPortal}));
}

render() {
const {showPortal} = this.state;
return (
<ExampleContext.Provider value="context from Example">
<div>
This is the parent comnponent.
<input type="button" value={showPortal ? "Hide" : "Show"} onClick={this.showHideClick}/>
{showPortal && <MyPortal />}
</div>
</ExampleContext.Provider>
);
}
}

class MyPortal extends React.Component {
static contextType = ExampleContext;
render() {
return ReactDOM.createPortal(
<div>This is "my portal," context stuff is "{this.context}"</div>,
document.getElementById("portal")
);
}
}

ReactDOM.render(
<Example />,
document.getElementById("root")
);
<div id="root"></div>
<hr>
<div id="portal"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

关于javascript - 是否有可能将 react 上下文注入(inject) react 中根外的div(在 body 下方),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57477604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com