使用这个库- https://tintef.github.io/react-google-places-autocomplete/docs/ -I有-
<GooglePlacesAutocomplete
apiKey={'someKey'}
autocompletionRequest={{
componentRestrictions: {
country: ['uk']
}
}}
selectProps={{
value: address,
onChange: (o) => {
let placeId = o["value"]["place_id"];
setAddress(o);
formik.setFieldValue("googlePlaceId", placeId);
}
}}
/>我需要传递什么作为"address“的值才能有一个初始值?
我尝试了{label:"some address",place_id:"ChIJNYiUp8ROeEgRikq4Ws76OpU"},并使用实用程序geocodeByPlaceId传递返回的对象。到目前为止,前者在初始化时在输入字段中显示了标签,但它看起来是损坏的。例如,当我尝试删除带有退格键的值时,我的react应用程序崩溃,并在控制台中显示以下错误-
Uncaught TypeError: Cannot read property 'place_id' of undefined
at Object.getOptionValue (index.es.js:30)
at cr (index.es.js:30)
at eval (index.es.js:30)
at Array.some (<anonymous>)
at lr (index.es.js:30)
at or (index.es.js:30)
at eval (index.es.js:30)
at Array.map (<anonymous>)
at rr (index.es.js:30)
at eval (index.es.js:30)发布于 2021-08-12 14:05:01
令人恼火的是,这个库的文档一个引用了另一个的文档,react-select。
这就是我的组件现在的样子-
<GooglePlacesAutocomplete
apiKey={''}
autocompletionRequest={{
componentRestrictions: {
country: ['uk']
}
}}
selectProps={{
defaultInputValue: formik.status["addressLabel"],
isClearable: true,
value: address,
onChange: (o) => {
let placeId = "";
if(o){
placeId = o["value"]["place_id"];
}
setAddress(o);
formik.setFieldValue("googlePlaceId",placeId);
}
}}
/>所以在更高的部分我有-
const [address, setAddress] = useState();默认初始值设置如下(使用formik) -
<Formik
initialValues={initialValues}
validationSchema={validationSchema}
// validationSchema={{}}
onSubmit={(values, actions) => {
actions.setSubmitting(false);
submitHandler(values, actions)
}}
initialStatus={{
addressLabel: addressLabel
}}
>
{
formik => {
return (
<Form formik={formik} {...formProps} />
)
}
}
</Formik>https://stackoverflow.com/questions/68755252
复制相似问题