簡體   English   中英

強制尚未安裝到 DOM 渲染的 React 組件?

[英]Force React component that have not mounted to DOM render?

我有一個 class,它是 React 的一個組件。 如何強制 class 呈現並返回給我一個 DOM 元素?

例如:

class MyComponent extends React.Component{...}
const myVirtualDOMComponentButNotMountToAnywhere = <MyComponent {...someprops} />

// is there any function that
const myDOMElement = MyComponent.createDOMElement();
// or
const myDOMElement = myVirtualDOMComponentButNotMountToAnywhere.renderAsDOMElement();
// return a result that similar to
document.createElement(...);

作為我的 function 的結果,我希望該 DOM 元素對該 DOM 元素執行自定義安裝(或我想要的任何操作),我希望這類似於document.createElement()的結果。

另一個作為真實用例的例子:

import { Link } from 'react-router-dom';
const myLink = <Link to='/...' />
const myDOMLink = myLink.renderAsDOMElement();
document.getElementById('someId').appendChild(myDOMLink);

可能是我想太多了。 它是作為react-dom的 API 提供的: render

import ReactDOM from 'react-dom';
ReactDOM.render(<MyComponent />, document.getElementById('someId'))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM