【问题标题】:Updating the value of a react-final-form Field component with a onSelect function call?使用 onSelect 函数调用更新 react-final-form Field 组件的值?
【发布时间】:2019-10-29 14:57:38
【问题描述】:

我正在使用 typescript 和 react-final-form 做一个反应应用程序,我正在使用一些反应库来集成谷歌位置和地理编码器 API:“react-places-autocomplete”:“^7.2 .1""react-final-form": "^6.3.0"

我遇到的问题是在用户选择 onSelect 上的自动完成组件提供的建议之一后更新 react-final-form 字段中的值:以下是代码的一些摘录:

onSelect 函数:

const handleCitySelect = (selectedCity: string) => {
    geocodeByAddress(selectedCity)
      .then(results => getLatLng(results[0]))
      .then(latlng => {
        setCityLatLng(latlng);
      }); <-- Here I need to update the field with selectedCity
  };

  const handleVenueSelect = (selectedVenue: string) => {
    geocodeByAddress(selectedVenue)
      .then(results => getLatLng(results[0]))
      .then(latlng => {
        setVenueLatLng(latlng);
      }); <-- Here I need to update the field with selectedVenue
  };

反应最终形式:

      <FinalForm <-- react-final-form component

        validate={validate}
        initialValues={activity}
        onSubmit={handleFinalFormSubmit}
        render={({ handleSubmit, invalid, pristine }) => (
          <Form onSubmit={handleSubmit} loading={loading}> <-- semantic-ui-react component
            <Field
              name="title"
              placeholder="Title"
              value={activity.title}
              component={TextInput}
            />
            <Field
              name="description"
              placeholder="Description"
              rows={3}
              value={activity.description}
              component={TextAreaInput}
            />
            <Field
              component={SelectInput}
              options={category}
              name="category"
              placeholder="Category"
              value={activity.category}
            />
            <Form.Group widths="equal">
              <Field
                component={DateInput}
                name="date"
                date={true}
                placeholder="Date"
                value={activity.date}
              />
              <Field
                component={DateInput}
                name="time"
                time={true}
                placeholder="Time"
                value={activity.time}
              />
            </Form.Group>

            <Field <-- field I need to update after the onSelect callback
              component={PlaceInput}
              name="city"
              placeholder="City"
              options={{ types: ["(cities)"] }}
              value={activity.city}
              onSelect={handleCitySelect}
            />
            <Field <-- field I need to update after the onSelect callback
              component={PlaceInput}
              name="venue"
              placeholder="Venue"
              options={{
                location: new google.maps.LatLng(cityLatLng),
                radius: 1000,
                types: ["establishment"]
              }}
              value={activity.venue}
              onSelect={handleVenueSelect}
            />
<...>

PlaceInput 是一个自定义组件,它作为 PlacesAutocomplete 组件的包装服务,根据用户键入的内容提供建议。

感谢任何可以提出解决方案的人。我搜索了互联网,但找不到快速解决我认为应该不费吹灰之力的事情。如果需要更多解释,请告诉我或更多代码摘录。

干杯,

【问题讨论】:

    标签: reactjs typescript google-places-api semantic-ui-react react-final-form


    【解决方案1】:

    要设置字段值,您的PlaceInput 需要调用input.onChange(newValue)

    另外,您不应该将 value 属性传递给 FieldThat's only for checkboxes and radio buttons.

    【讨论】:

    • 您好 Erik,感谢您的回复,但您的建议对我来说仍然不是很清楚。我确实通过了 input.onChange 但我不确定应该在哪里调用该方法。这是 github [link]github.com/luisfguzman/Reactivities/blob/master/client-app/src/… 的链接,如果您可以查看的话。您还说不要将 value 属性传递给 Field,但我使用这个组件 ActivityForm 来创建和编辑 Activity,那么当用户编辑 Activity 时初始化数据的正确方法是什么?再次感谢您的帮助,非常感谢!
    • 嗯...沙箱在这里真的很有帮助。
    • 嗨 Erik,我为客户端应用程序创建了一个沙箱,这是我的 react-typescrit 前端:[link]codesandbox.io/s/github/luisfguzman/Reactivities/tree/master/… 但它没有连接到作为 DotNetCore 的 API应用。我已将其部署到 azure 以进行完全集成:[link]intermingle.azurewebsites.net 我遇到的问题是在创建/编辑和活动时,当用户选择其中一个建议时,表单的 City/Venue 字段不会更新.让我知道这是否有帮助。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多