-
Notifications
You must be signed in to change notification settings - Fork 47.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I get DOM on the React Hook? #16699
Comments
Hello @karmiy! Accessing DOM elements of function components is handled with refs. You should look into useRef hook and Forwarding Refs article for more info. |
Thanks for your answer! function Collapse(props) { |
I don't want to add any extra elements like this: function Collapse(props) { I don't want to put this DIV element on the outside. |
I think you will be interested in reactjs/rfcs#97 then, it's a proposal about fragment references which specifically allows you to avoid introducing wrapper containers. I also have a code sample for you, |
I'm closing this since it isn't a bug or feature request per se, but feel free to carry on the conversation. I would also recommend using one of our community resources https://reactjs.org/community/support.html. |
Now it seems that ref cannot be used in <Fragment> like: |
That is why I also provided a link to RFC about Fragment refs :) |
Is there a better solution? |
In the class component,we can use ReactDom.findDOMNode(this) to get DOM element:
class Collapse {
componentDidMount() {
ReactDom.findDOMNode(this) ;
}
return this.props.children;
}
But how can I get it in React Hook without 'this';
The text was updated successfully, but these errors were encountered: