简体   繁体   English

React-final-form - 将 state 转换为表单值

[英]React-final-form - get state into form values

I want to 'plug in' a map location picker into some of my forms to add longitude and latitude into the form values.我想将 map 位置选择器“插入”到我的一些 forms 中,以将longitudelatitude添加到表单值中。

I created this sandbox to demonstrate the difficulties I'm having.我创建了这个沙箱来展示我遇到的困难。

I can get the map position into state as the user pans the map, and render the values OK, but I can't get these values into the fields using I can get the map position into state as the user pans the map, and render the values OK, but I can't get these values into the fields using

<Field
 value={position.lat.toFixed(5)}
 ...
/>

I need the location picker component Geo.jsx to be a separate reusable component to 'plug in' to any react-final-form Form if needed.我需要位置选择器组件Geo.jsx作为单独的可重用组件,以便在需要时“插入”到任何 react-final-form Form I also need the latitude and longitude inputs visible and editable for cases when the user wants to manually input these values.当用户想要手动输入这些值时,我还需要可见和可编辑的latitudelongitude输入。

If anyone can see where I'm going wrong with this, it would be very much appreciated.如果有人能看到我在哪里出错,将不胜感激。

I fixed this by simply changing value to defaultValue .我通过简单地将value更改为defaultValue来解决这个问题。

On further reading I realised that value is only for checkboxes and radio buttons.进一步阅读时,我意识到该value仅适用于复选框和单选按钮。

Change改变

      <Field
        name="longitude"
        label="Longitude"
        type="number"
        parse={(value) => Number(value)}
        component="input"
     -  value={position.lng.toFixed(5)}
        helper="At map center"
      />

to

      <Field
        name="longitude"
        label="Longitude"
        type="number"
        parse={(value) => Number(value)}
        component="input"
     +  defaultValue={position.lng.toFixed(5)}
        helper="At map center"
      />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM