英文:
Rendering a div element inside existing div during page rendering
问题
我卡在如何在一个 div 内呈现一个组件。我的代码如下,我想在渲染时将具有类名 RequestNewOrgBtn 的 div 呈现在 parentDiv 内。我应该如何在渲染时实现这个目标?
const labelItem = Array.from(document.querySelectorAll('label'))
.find(el => el.textContent === 'External Org Name');
const parentDiv = labelItem?.parentNode?.parentNode?.parentNode;
return (
<>
{
// 在 parentDiv 内呈现以下 div
<div className="RequestNewOrgBtn">
<Button disabled={false} onClick={onAddClick}>
{"Request New External org"}
</Button>
<RequestExternalOrgModal
isOpen={ShowReqExOrgModal}
onClose={onReqExOrgModalClose} />
</div>
}
</>
);
英文:
I'm stuck at rendering a component insider a div. My code is like below, I want to render the div with classname RequestNewOrgBtn inside the parentDiv. How can I do it at runtime of render?
const labelItem = Array.from(document.querySelectorAll('label'))
.find(el => el.textContent === 'External Org Name');
const parentDiv = labelItem?.parentNode?.parentNode?.parentNode;
return(
<>
{
//render below div inside parentDiv
<div className="RequestNewOrgBtn">
<Button disabled={false} onClick={onAddClick}>
{ "Request New External org" }
</Button>
<RequestExternalOrgModal
isOpen={ShowReqExOrgModal}
onClose={onReqExOrgModalClose} />
</div>
}
</>
);
答案1
得分: 1
可能最简单的方法是将React应用程序的适当部分渲染到相关元素中,类似于将<App />
渲染到_root_元素中。
例如...
import { createRoot } from "react-dom/client";
import RequestNewOrgBtn from "./RequestNewOrgBtn"; // 你的组件
const labelItem = Array.from(document.querySelectorAll("label")).find(
(el) => el.textContent === "External Org Name"
);
const parentDiv = labelItem?.parentNode?.parentNode?.parentNode;
if (parentDiv) {
// 为你的按钮组件创建一个新的根元素
const btnRootElement = document.createElement("div");
// 将其附加到找到的`parentDiv`中
parentDiv.appendChild(btnRootElement);
const btnRoot = createRoot(btnRootElement);
btnRoot.render(<RequestNewOrgBtn />);
}
对于React 16,你只需使用ReactDOM
的等价物
import ReactDOM from "react-dom";
// ...
ReactDOM.render(<RequestNewOrgBtn />, btnRootElement);
英文:
Probably the easiest way to do this would be to render the appropriate parts of your React app into the element in question, similar to how you would render <App />
into your root element.
For example...
import { createRoot } from "react-dom/client";
import RequestNewOrgBtn from "./RequestNewOrgBtn"; // your component
const labelItem = Array.from(document.querySelectorAll("label")).find(
(el) => el.textContent === "External Org Name"
);
const parentDiv = labelItem?.parentNode?.parentNode?.parentNode;
if (parentDiv) {
// create a new root for your button component
const btnRootElement = document.createElement("div");
// append it into the found `parentDiv`
parentDiv.appendChild(btnRootElement);
const btnRoot = createRoot(btnRootElement);
btnRoot.render(<RequestNewOrgBtn />);
}
For React 16, you just use the ReactDOM
equivalent
import ReactDOM from "react-dom";
// ...
ReactDOM.render(<RequestNewOrgBtn />, btnRootElement);
答案2
得分: -1
这有点含糊不清。但是,如果我理解正确的话,我认为你可以将innerHTML设置为追加这个div:
parentDiv.innerHTML += "<div>Your div content here</div>";
更好的方法是,将你的div设置为一个变量,然后使用变量进行追加,使代码更清晰:
const myDiv = "<div>Your div content here</div>";
parentDiv.innerHTML += myDiv;
我使用了这个链接来确保我记得正确。
英文:
This is a little ambiguous. But, if I am understanding correctly, I think you can set the innerHTML to append the div
parentDiv.innerHTML += "<div>Your div content here</div>"
better yet, set your div as a variable and then just += the variable for cleaner code.
const myDiv = "<div>Your div content here</div>"
parentDiv.innerHTML += myDiv
Used this to make sure I was remembering correctly
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论