How to pass value from on component to Redux form


user_3535

I'm using Redux forms for forms in React.js, my form is, and I have a custom Google Map component, I want to bind lat and long to my form

form

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

My MyParentComponentWrapper code is

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;

When the user drags the marker, the component will console.log the latitude and longitude values

How to pass values console.log​​to redux-form?

Can someone suggest a way?

Norian Nicks

This is the codeandbox of the application used redux-form. Note that I use reduxForm's action in your map container when you move the marker after setting the form latLngForm.jsin . That's what the update store is all about.connectdispatchchange

I also passed positionin a place propto <MyMapComponent />set your markup. This means that the marker's position is always based on the form's value, and moving the map's marker manually changes the form's value. This allows you to manually set the position by field or by dragging and dropping markers.

mapStateToPropsImportant here <MyMapComponent />. redux-formThe value in the state is automatically stored for us, which is where we retrieve it.

Note these lines at the top of the file:

import { change, formValueSelector } from "redux-form";
...
const formSelector = formValueSelector("form");

This will set up our form selector. "form"is the identifier of the form. Now, to retrieve these values ​​from our state, we do:

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

Then we connectactually connect the component to the store:

connect(mapStateToProps)(MyMapComponent);

Redux worked its magic and now our fields are available in our this.props.positioncomponents !

Related


How to pass value from on component to Redux form

user_3535 I'm using Redux forms for forms in React.js, my form is, and I have a custom Google Map component, I want to bind lat and long to my form form import React from 'react'; import { Field, reduxForm } from 'redux-form'; const SimpleForm = props => { c

How to pass value from on component to Redux form

user_3535 I'm using Redux forms for forms in React.js, my form is, and I have a custom Google Map component, I want to bind lat and long to my form form import React from 'react'; import { Field, reduxForm } from 'redux-form'; const SimpleForm = props => { c

How to pass value from on component to Redux form

user_3535 I'm using Redux forms for forms in React.js, my form is, and I have a custom Google Map component, I want to bind lat and long to my form form import React from 'react'; import { Field, reduxForm } from 'redux-form'; const SimpleForm = props => { c

How to pass value from on component to Redux form

user_3535 I'm using Redux forms for forms in React.js, my form is, and I have a custom Google Map component, I want to bind lat and long to my form form import React from 'react'; import { Field, reduxForm } from 'redux-form'; const SimpleForm = props => { c

How to pass value from on component to Redux form

user_3535 I'm using Redux forms for forms in React.js, my form is, and I have a custom Google Map component, I want to bind lat and long to my form form import React from 'react'; import { Field, reduxForm } from 'redux-form'; const SimpleForm = props => { c

How to pass value from on component to Redux form

user_3535 I'm using Redux forms for forms in React.js, my form is, and I have a custom Google Map component, I want to bind lat and long to my form form import React from 'react'; import { Field, reduxForm } from 'redux-form'; const SimpleForm = props => { c

How to pass value from custom component class to redux-form?

bluprince13 I am calling a custom component in redux form. <Field name="myField" component={SiteProjectSelect}/> This component is a combination of two combo boxes. The second box depends on the value of the first box - i.e. depending on the site you choose,

How to pass value from custom component class to redux-form?

bluprince13 I am calling a custom component in redux form. <Field name="myField" component={SiteProjectSelect}/> This component is a combination of two combo boxes. The second box depends on the value of the first box - i.e. depending on the site you choose,

How to pass form value from child component to parent component

Indrjit Kumar Is there a way to pass form data from child component to parent component where submit button has been kept in parent component. Note:- I don't want to use ref for this because ref will waste a lot of memory and I may have 6-7 children in my pare

How to pass form value from child component to parent component

Indrjit Kumar Is there a way to pass form data from child component to parent component where submit button has been kept in parent component. Note:- I don't want to use ref for this because ref will waste a lot of memory and I may have 6-7 children in my pare

Pass value from component to Redux Store

abu abu I am learning Redux using React. I have a Form in a component. I want to pass data from this component to a Redux action. I am using the following code to submit the form. handleSubmit = event => { var mouse = 'mouse'; this.props.dispatch(mouse

Pass value from component to Redux Store

abu abu I am learning Redux using React. I have a Form in a component. I want to pass data from this component to a Redux action. I am using the following code to submit the form. handleSubmit = event => { var mouse = 'mouse'; this.props.dispatch(mouse

How to pass data from redux-form into component via redux-saga?

ryoshpa I am trying to figure out how to redux-formuse redux-saga. Expected traffic: I have an redux-forminternal selectboxwhich includes two options. Selecting an option will trigger an API call to request data. handleOptionSagaThis data will be processed and

How to pass data from redux-form into component via redux-saga?

ryoshpa I am trying to figure out how to redux-formuse redux-saga. Expected traffic: I have an redux-forminternal selectboxwhich includes two options. Selecting an option will trigger an API call to request data. handleOptionSagaThis data will be processed and

How to pass initial value to FieldArray component in redux?

Kayana Harley I have an array of objects and want to pass data as initial data to FieldArray component of redux form. I went through the documentation about the FieldArray component , but it's not clear if an initial value is provided. Is there a way to do thi

How to pass initial value to FieldArray component in redux?

Kayana Harley I have an array of objects and want to pass data as initial data to FieldArray component of redux form. I went through the documentation about the FieldArray component , but it's not clear if an initial value is provided. Is there a way to do thi

How to pass default value to Redux form

Aditya Kumar I have built a redux form which is a user profile form where the user can edit the profile details for this. I would like to put the initial value into the backend, after which they can also edit it. But I tried the way the documentation says http

How to pass default value to Redux form

Aditya Kumar I have built a redux form which is a user profile form where the user can edit the profile details for this. I would like to put the initial value into the backend, after which they can also edit it. But I tried the way the documentation says http

How to pass default value to Redux form

Aditya Kumar I have built a redux form which is a user profile form where the user can edit the profile details for this. I would like to put the initial value into the backend, after which they can also edit it. But I tried the way the documentation says http

How to pass default value to Redux form

Aditya Kumar I have built a redux form which is a user profile form where the user can edit the profile details for this. I would like to put the initial value into the backend, after which they can also edit it. But I tried the way the docs say https://redux-

How to pass default value to Redux form

Aditya Kumar I have built a redux form which is a user profile form where the user can edit the profile details for this. I would like to put the initial value into the backend, after which they can also edit it. But I tried the way the docs say https://redux-

How to pass default value to Redux form

Aditya Kumar I have built a redux form which is a user profile form where the user can edit the profile details for this. I would like to put the initial value into the backend, after which they can also edit it. But I tried the way the docs say https://redux-

How to pass default value to Redux form

Aditya Kumar I have built a redux form which is a user profile form where the user can edit the profile details for this. I would like to put the initial value into the backend, after which they can also edit it. But I tried the way the docs say https://redux-

How to pass default value to Redux form

Aditya Kumar I have built a redux form which is a user profile form where the user can edit the profile details for this. I would like to put the initial value into the backend, after which they can also edit it. But I tried the way the docs say https://redux-

How to pass component props from React to Redux?

Dongyob When trying to pass a prop (just a string) of a component (from a table component) to redux via a connection, I keep getting the error, The error output I get is: TypeError: Cannot read property 'props' of undefined 13 | 14 | const getTableData =

How to pass component props from React to Redux?

Dongyob When trying to pass a prop (just a string) of a component (from a table component) to redux via a connection, I keep getting the error, The error output I get is: TypeError: Cannot read property 'props' of undefined 13 | 14 | const getTableData =

How to pass parameters from component to redux action

H. Harrison 1993 I'm trying to pass the country name from a React component to an action in Redux, but when it reaches the action creator, it becomes undefined. I think it has something to do with the mapDispatchToProps() section below the component, but I'm p