Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing refs from Component to Component in React

So I am not sure if passing refs would be the best thing to do but it's kinda what I have set-out to do tell me if there is a better option..

So I am trying to have an onClick of a nav link, scroll down to the the div "contactForm".

App.js

import ContactForm from './components/ContactForm'
import ParllaxPage from './components/ParllaxPage'
import NavigationBar from './components/NavigationBar'
import React from 'react';
import './App.css';

const App = () => {


  return (

    < div cssClass="App" >
      <body>
        <span><NavigationBar /></span>
        <ParllaxPage cssClass="parallax-wrapper" />
        <ParllaxPage cssClass="parallax-wrapper parallax-pageOne" />
        <ContactForm />
      </body >
    </div >
  );
}

export default App;

I was trying to use forwardRef but I am not sure that I was doing it correctly so...

NavigationBar.js

import ContactForm from "./ContactForm";
import React, { useRef } from "react";
import App from "../App";
import { Nav, Navbar, Form, FormControl, Button } from "react-bootstrap";

const ContactFormRef = React.forwardRef((props, ref) => (
 <ContactForm className="contactForm" ref={ref}>
  {props.children}
 </ContactForm>
));

const scrollToRef = (ref) => ref.current.scrollIntoView({ behavior: "smooth" });

const NavigationBar = () => {
 const ref = React.forwardRef(ContactFormRef);

 return (
  <Navbar bg="light" expand="lg">
   <Navbar.Brand href="#home">A1 Gutters</Navbar.Brand>
   <Navbar.Toggle aria-controls="b casic-navbar-nav" />
   <Nav className="mr-auto">
    <Nav.Link href="#home">Home</Nav.Link>
    <Nav.Link href="#link">Link</Nav.Link>
    <Nav.Link href="#" onClick={console.log(ref)}>
     Contact
    </Nav.Link>
   </Nav>
  </Navbar>
 );
};

export default NavigationBar;

I don't think the other files really need to be shown, I am just trying to get the className out of the ContactForm component so I can scroll to it onClick.. I currently just have a console.log in the onClick.

like image 425
Blue Avatar asked Apr 29 '26 09:04

Blue


2 Answers

Using Hooks will simplify here.

  1. Have state variable for gotoContact and ref for contactRef
  2. Add click handler for navigation link contact
  3. Add useEffect hook and when ever use click on contact and ref is available (value in ref.current) then call the scroll to view)
import ContactForm from "./components/ContactForm";
import ParllaxPage from "./components/ParllaxPage";
import React, { useState, useEffect, useRef } from "react";
import "./App.css";

const NavigationBar = ({ onClickContact }) => {
  return (
    <Navbar bg="light" expand="lg">
      <Navbar.Brand href="#home">A1 Gutters</Navbar.Brand>
      <Navbar.Toggle aria-controls="b casic-navbar-nav" />
      <Nav className="mr-auto">
        <Nav.Link href="#home">Home</Nav.Link>
        <Nav.Link href="#link">Link</Nav.Link>
        <Nav.Link href="#" onClick={() => onClickContact()}>
          Contact
        </Nav.Link>
      </Nav>
    </Navbar>
  );
};

const App = () => {
  const [gotoContact, setGotoContact] = useState(false);
  const contactRef = useRef(null);

  useEffect(() => {
    if (gotoContact && contactRef.current) {
      contactRef.current.scrollIntoView();
      setGotoContact(false);
    }
  }, [gotoContact, contactRef.current]);

  return (
    <div cssClass="App">
      <body>
        <span>
          <NavigationBar onClickContact={() => setGotoContact(true)} />
        </span>
        <ParllaxPage cssClass="parallax-wrapper" />
        <ParllaxPage cssClass="parallax-wrapper parallax-pageOne" />
        <div ref={contactRef}>
          <ContactForm />
        </div>
      </body>
    </div>
  );
};

export default App;
like image 143
Siva K V Avatar answered May 01 '26 00:05

Siva K V


You should identify the div "contactForm" with an id and have an anchor tag point to it:

<a href="#contactForm"></a>
<div id="contactForm"></div>

You can add scroll-behaviour: smooth to the body in CSS

like image 45
Nils Schwebel Avatar answered May 01 '26 00:05

Nils Schwebel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!