this.props 不是一个函数 - 反应形式

发布于 2025-01-15 12:32:10 字数 4282 浏览 0 评论 0原文

我正在尝试在我的反应表单的输入中使用谷歌地图地址自动完成功能。

当我单击它返回的地址时,输入的状态不会更新。

import React from "react";
import { Field } 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();
    // issue here
    this.props.onPlaceLoaded(place);
    // issue here
  }

  render() {
    return (
      <Field
        innerRef={this.autocompleteInput}
        id="autocomplete"
        type="text"
        name="address"
        className="form-control"
        placeholder=""
      />
    );
  }
}

export default SearchBar;

如何更新状态以存储所选地址?

编辑以显示我的搜索栏组件的使用方式:

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>
                      
<SearchBar />

                        <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;

包括更多信息,以便堆栈溢出可以让我发布

I'm trying to use the google maps address autocomplete in an input on my react form.

When I click an address it returns, the state of the input is not updated.

import React from "react";
import { Field } 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();
    // issue here
    this.props.onPlaceLoaded(place);
    // issue here
  }

  render() {
    return (
      <Field
        innerRef={this.autocompleteInput}
        id="autocomplete"
        type="text"
        name="address"
        className="form-control"
        placeholder=""
      />
    );
  }
}

export default SearchBar;

How can I update a state to store the selected address?

Edit to show how my Search bar component is being used:

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>
                      
<SearchBar />

                        <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;

Including more info so stack overflow will let me post

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半仙 2025-01-22 12:32:10
import Geocode from "react-geocode";

handlePlaceChanged() {
    const place = this.autocomplete.getPlace().formatted_address;
    // issue here
    // this.props.onPlaceLoaded(place);
    // issue here
    Geocode.fromAddress(place).then( response => {
       const { lat, lng } = response.results[0].geometry.location;

       // you need this :
       this.setState({
           place: place,
           lat: lat,
           lng: lng
       });
  },
  error => {
    console.error(error);
  }
);


}
import Geocode from "react-geocode";

handlePlaceChanged() {
    const place = this.autocomplete.getPlace().formatted_address;
    // issue here
    // this.props.onPlaceLoaded(place);
    // issue here
    Geocode.fromAddress(place).then( response => {
       const { lat, lng } = response.results[0].geometry.location;

       // you need this :
       this.setState({
           place: place,
           lat: lat,
           lng: lng
       });
  },
  error => {
    console.error(error);
  }
);


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文