admin管理员组文章数量:1279048
I have a page with a form rendered in the server, it handles validation, and the correct value for the selects.
I want to hide the DOM of that form, and append it into a react ponent so I can use it in react-router.
const NewItem = React.createClass({
render() {
return (
<div>
<h1>New item</h1>
{/* I WANT THE FORM FROM THE PAGE HERE*/}
</div>
)
}
})
What is the best way to do it?
I have a page with a form rendered in the server, it handles validation, and the correct value for the selects.
I want to hide the DOM of that form, and append it into a react ponent so I can use it in react-router.
const NewItem = React.createClass({
render() {
return (
<div>
<h1>New item</h1>
{/* I WANT THE FORM FROM THE PAGE HERE*/}
</div>
)
}
})
What is the best way to do it?
Share Improve this question asked Sep 29, 2015 at 16:29 Adrian RibaoAdrian Ribao 1,4393 gold badges11 silver badges9 bronze badges3 Answers
Reset to default 2You have full access to the DOM in ponentDidMount
. You can use refs to access the specific DOM element you want.
var NewItem = React.createClass({
ponentDidMount: function () {
this.refs.formTarget.appendChild(myFormDomElement);
},
render: function () {
return React.DOM.div(null,
React.DOM.h1(null, "New item"),
React.DOM.div({ref: "formTarget"}));
}
});
Note that in 0.14, a ref is a raw DOM element. Prior to that a ref was a react ponent and you had to call React.findDOMNode(it)
to get the actual DOM element.
React > 16.3
Try using portals like in this ponent:
import {Component} from 'react';
import {createPortal} from 'react-dom';
import PropTypes from 'prop-types';
class DOMPortal extends Component {
constructor(props) {
super(props);
this.el = document.createElement(props.ponent);
}
ponentDidMount() {
this.props.parentEl.appendChild(this.el);
}
ponentWillUnmount() {
this.props.parentEl.removeChild(this.el);
}
render() {
return createPortal(
this.props.children,
this.el,
);
}
}
DOMPortal.propTypes = {
parentEl: PropTypes.object.isRequired,
ponent: PropTypes.string,
};
DOMPortal.defaultProps = {
ponent: 'div',
};
Now you can pass your external DOM reference as the parentEl props to it:
<DOMPortal parentEl={decorator.contentWidget.domNode}>...children</DOMPortal>
React < 16.3
Using this.refs
is "deprecated", try this instead :
render() {
return <div ref={(DOMNodeRef) => {
this.ponentRef=DOMNodeRef;
}}>
...
</div>;
}
Then this.ponentRef
will be accesible in ponentDidMount()
so you can append your external DOM element:
ponentDidMount(){
this.ponentRef.appendChild(externalDOMelement);
}
Notes:
Remember that this.ponentRef
changes over time (renders()), so you must update it wherever you are passing it to.
Check for a defined reference before using it: if(this.ponentRef){// ... your code}
Functional Components' refs are handled differently.
Source:
React Doc
React gives us the functionality dangerouslySetInnerHTML. it gives us the support of adding HTML element. for example
function createMarkup() {
return {__html: 'First · Second'};
}
function MyComponent() {
return <div dangerouslySetInnerHTML={createMarkup()} />;
}
You can also use portals introduced in react-16 for appending React ponent anywhere in the tree by using following code.
ReactDOM.createPortal(child, container)
follow following link Portals for refference
本文标签: javascriptHow can I append external DOM to React componentStack Overflow
版权声明:本文标题:javascript - How can I append external DOM to React component? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741260957a2367610.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论