Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

I’m trying to create a sign up form with an input for a users address. The address input uses the google autocomplete address api.

I’d like to be able to keep it as a Formik field, so I can use Yup validation on it.

The address input component looks like

// Google.jsx
import React from "react";
import { Formik, Form, Field, ErrorMessage } from "formik";
/* global google */


class SearchBar extends React.Component {
  constructor(props) {
    super(props);
    this.autocompleteInput = React.createRef();
    this.autocomplete = null;
    this.handlePlaceChanged = this.handlePlaceChanged.bind(this);
  }

  componentDidMount() {
    this.autocomplete = new google.maps.places.Autocomplete(this.autocompleteInput.current,
        {"types": ["address"]});
    this.autocomplete.addListener('place_changed', this.handlePlaceChanged);
  }


  handlePlaceChanged(){
    const place = this.autocomplete.getPlace();
    console.log(place);

  }

  render() {
    return (
        <Field ref={this.autocompleteInput}  id="autocomplete" type="text" name="address" placeholder="" />
    );
  }
}


export default SearchBar;

And my Form component looks like:

import React from "react";
import { Formik, Form, Field, ErrorMessage } from "formik";
import * as Yup from "yup";
import SearchBar from "./Google";


const LoginSchema = Yup.object().shape({
  fName: Yup.string().required("Please enter your first name"),
  address: Yup.string().required("invalid address"),
});

class Basic extends React.Component {
  render() {
    return (
      <div className="container">
        <div className="row">
          <div className="col-lg-12">
            <Formik
              initialValues={{
                fName: "",
                postal: "",
              }}
              validationSchema={LoginSchema}
              onSubmit={(values) => {
                console.log(values);
                console.log("form submitted");
              }}
            >
              {({ touched, errors, isSubmitting, values }) =>
                !isSubmitting ? (
                  <div>
                    <div className="row mb-5">
                      <div className="col-lg-12 text-center">
                        <h1 className="mt-5">LoKnow Form</h1>
                      </div>
                    </div>

                    <Form>

                      <div className="form-group">
                        <label htmlFor="fName">First Name</label>
                        <Field
                          type="text"
                          name="fName"
                          className={`mt-2 form-control
                    ${touched.fName && errors.fName ? "is-invalid" : ""}`}
                        />

                        <ErrorMessage
                          component="div"
                          name="fName"
                          className="invalid-feedback"
                        />
                      </div>

                      <div className="form-group">
                        <label htmlFor="address">Address</label>

                        <Field name="address" component={SearchBar} placeholder="" />

                        <ErrorMessage
                          component="div"
                          name="address"
                          className="invalid-feedback"
                        />

                      </div>



                      <button
                        type="submit"
                        className="btn btn-primary btn-block mt-4"
                      >
                        Submit
                      </button>
                    </Form>
                  </div>
                ) : (
                  <div>
                    <h1 className="p-3 mt-5">Form Submitted</h1>

                    <div className="alert alert-success mt-3">
                      Thank for your connecting with us.
                    </div>
                  </div>
                )
              }
            </Formik>
          </div>
        </div>
      </div>
    );
  }
}

export default Basic;

This returns an error of “Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?”.

Which is coming from my address input component at: <Field ref={this.autocompleteInput} id="autocomplete" type="text" name="address" placeholder="" />

Everything else is working, I just need to get past this last hurdle and I’ll be good from here.

I will begin looking into the docs, but I’m unfortunately in a rush to get this done so I figured I’d try my luck here!

Any help is greatly appreciated! Thank you!