Answer the question
In order to leave comments, you need to log in
How to pass a ref to another component?
Hello.
There are two different components. You need to implement something like this:
class App extends React.Component {
bottomRef = React.createRef();
onClick = () => {
this.bottomRef.current.scrollIntoView();
};
render() {
return (
<div>
<button onClick={this.onClick}>Scroll to bottom</button>
<div style={{ height: 2000 }} />
<div ref={this.bottomRef}>bottom</div>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));
Answer the question
In order to leave comments, you need to log in
You need to use either forwardRef, or pass ref through props (only use the prop name not ref, but for example innerRef) The
first option will be preferable and more correct.
https://ru.reactjs.org/docs/forwarding-refs.html#f...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question