Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

useRef ref.current is undefined passed as prop and through context

Trying to pass a ref from one component to another and only trying to console log the ref.current property and it constantly comes up as undefined. I have tried this question’s solution and it is still undefined.

I only want to update the class of an element in the Header component once an event is triggered in the SearchBar component. So I am passing the ref down to SearchBar

The Header is where the ref is coming from and is being passed to the SearchBar component.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have tried to pass it as a prop from the Header to the SearchBar and now I am trying to pass it through state containing the ref. I have console logged the logo.current through the passed prop and through the state.current property and both come back undefined.

import React, { useContext, useEffect, useRef } from "react";
import SearchContext from "../contexts/SearchStore";
import SearchBar from "./SearchBar";

const Header = () => {
  const { setRefState, refState } = useContext(SearchContext);
  const logo = useRef();

  useEffect(() => {
    setRefState(logo.current);
  }, [logo.current])

  return (
    <section id="headerContainer" className="d-grid">
      <h2
        ref={logo}
        className="ui header d-flex align-items-center justify-content-center mb-0 headerLogo"
      >
        <div className="content headerFont fs-3 ps-0">WhatSong.</div>
        <i className="play circle icon fs-5"></i>
      </h2>
      <div id="header2" className="d-flex align-items-center flex-row-reverse">
        <SearchBar logo={logo} />
      </div>
    </section>
  );
};

export default Header;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
import React, { useEffect, useContext } from "react";
import SearchContext from "../contexts/SearchStore";
import "../styles/body.css";

const SearchBar = (logo) => {
  const {
    term,
    setTerm,
    submittedTerm,
    setSubmittedTerm,
    refState,
  } = useContext(SearchContext);

  const styleLogo = () => {
    console.log(refState.current);
    console.log(logo.current)
  };
  
    return (
    <div id="searchContainer" className="w-50 d-flex justify-content-end">
      <form
        onSubmit={(e) => {
          e.preventDefault();
          setSubmittedTerm(term);
        }}
        className="ui left icon input w-50"
      >
        <i className="inverted circular search link icon"></i>
        <input
          value={term}
          type="text"
          placeholder="Search..."
          data-dashlane-rid="3640789f2356683f"
          data-form-type=""
          className="searchInput"
          onChange={({ target }) => setTerm(target.value)}
          onMouseEnter={styleLogo}
          onMouseLeave={styleLogo}
          onFocus={styleLogo}
          onBlur={styleLogo}
        />
      </form>
    </div>
  );
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

>Solution :

You need to recieve logo correctly:

const SearchBar = ({logo}) => {
  const {
    term,
    setTerm,
    submittedTerm,
    setSubmittedTerm,
    refState,
  } = useContext(SearchContext);

  const styleLogo = () => {
    console.log(refState.current);
    console.log(logo.current)
  };
  
    return (
    <div id="searchContainer" className="w-50 d-flex justify-content-end">
      <form
        onSubmit={(e) => {
          e.preventDefault();
          setSubmittedTerm(term);
        }}
        className="ui left icon input w-50"
      >
        <i className="inverted circular search link icon"></i>
        <input
          value={term}
          type="text"
          placeholder="Search..."
          data-dashlane-rid="3640789f2356683f"
          data-form-type=""
          className="searchInput"
          onChange={({ target }) => setTerm(target.value)}
          onMouseEnter={styleLogo}
          onMouseLeave={styleLogo}
          onFocus={styleLogo}
          onBlur={styleLogo}
        />
      </form>
    </div>
  );
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading