如何将值从on组件传递到Redux表单

Use*_*535 10 javascript reactjs redux-form react-redux-form

我在React.js中使用Redux表单和我的表单,我有一个自定义谷歌地图组件我想绑定lat和long到我的表单

形成

import React from 'react';
import { Field, reduxForm } from 'redux-form';
const SimpleForm = props => {
  const { handleSubmit, pristine, reset, submitting } = props;
  return (
    <form onSubmit={handleSubmit}>
      <div className="position-relative form-group">
        <label>First Name</label>
        <div>
          <Field
            name="firstName"
            component="input"
            type="text"
            placeholder="First Name"
            className="form-control"
          />
        </div>
      </div>
<Field name = 'eventLocation'
        component = {MyParentComponentWrapper} />
</form>
  );
};
export default reduxForm({
  form: 'simple', // a unique identifier for this form
})(SimpleForm);
Run Code Online (Sandbox Code Playgroud)

我的MyParentComponentWrapper代码是

import React from 'react';
import { compose, withProps, lifecycle } from 'recompose';
import { withScriptjs, withGoogleMap, GoogleMap, Marker } from 'react-google-maps';

const MyMapComponent = compose(
    withProps({
        googleMapURL:
            'https://maps.googleapis.com/maps/api/js?key=AIzaSyCYSleVFeEf3RR8NlBy2_PzHECzPFFdEP0&libraries=geometry,drawing,places',
        loadingElement: <div style={{ height: `100%` }} />,
        containerElement: <div style={{ height: `400px` }} />,
        mapElement: <div style={{ height: `100%` }} />,
    }),
    lifecycle({
        componentWillMount() {
            const refs = {};

            this.setState({
                position: null,
                onMarkerMounted: ref => {
                    refs.marker = ref;
                },

                onPositionChanged: () => {
                    const position = refs.marker.getPosition();
                    console.log(position.toString());
                },
            });
        },
    }),
    withScriptjs,
    withGoogleMap
)(props => (
    <GoogleMap defaultZoom={8} defaultCenter={{ lat: -34.397, lng: 150.644 }}>
        {props.isMarkerShown && (
            <Marker
                position={{ lat: -34.397, lng: 150.644 }}
                draggable={true}
                ref={props.onMarkerMounted}
                onPositionChanged={props.onPositionChanged}
            />
        )}
    </GoogleMap>
));

class MyParentComponentWrapper extends React.PureComponent {
    state = {
        isMarkerShown: false,
    };

    render() {
        return (
            <div>
                <MyMapComponent isMarkerShown={true} />
            </div>
        );
    }
}

export default MyParentComponentWrapper;
Run Code Online (Sandbox Code Playgroud)

当用户拖动标记时,此组件将console.log用于lat和long值

如何将console.log值传递给redux-form?

任何人都可以建议一种方法吗?

Nor*_*Nyx 9

是使用a的应用的代码框redux-form.请注意,在设置表单后latLngForm.js,我会connect在您的地图容器中使用dispatchreduxForm change移动标记时的操作.这是更新商店的内容.

我还通过position在一个prop<MyMapComponent />设置你的标记的位置.这意味着您的标记的位置始终基于表单的值,并且手动移动地图的标记会更改表单的值.这将允许您通过字段手动设置位置,或通过拖放标记来设置位置.

mapStateToProps<MyMapComponent />这里是重要的一块.redux-form为我们自动存储状态值,这是我们检索它的地方.

请注意文件顶部的这些行:

import { change, formValueSelector } from "redux-form";
...
const formSelector = formValueSelector("form");
Run Code Online (Sandbox Code Playgroud)

这将设置我们的表单选择器."form"作为表单的标识符.现在,要从我们的州检索这些值,我们会:

const mapStateToProps = (state) => ({
    position: {
        lat: formSelector(state, 'lat'),
        lng: formSelector(state, 'lng') // The key here is the name you passed into the field.
    }
});
Run Code Online (Sandbox Code Playgroud)

然后我们connect用来实际将组件连接到商店:

connect(mapStateToProps)(MyMapComponent);
Run Code Online (Sandbox Code Playgroud)

Redux发挥其魔力,现在我们的领域可通过this.props.position我们的组件获得!