question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Initial value won't load in redux-form.

See original GitHub issue

Hi,

I’m using this together with redux-form v6, and i cannot make it load initial values:

I first tried this, as suggested in docs:

import Phone from 'react-phone-number-input'
import React from 'react';
import { Field } from 'redux-form';
import 'react-phone-number-input/style.css';

export default class MyPhoneInput extends React.Component {
	render() {
    	return (<Field name={props.name} component={Phone} />)
	}
}

But once i select a country from dropdown, i get error in console: error

So i tried wrapping it myself for redux-form like this:

import Phone from 'react-phone-number-input'
import React from 'react';
import { Field } from 'redux-form';
import 'react-phone-number-input/style.css';

export default class MyPhoneInput extends React.Component {
  constructor(props) {
    super(props);
    this.fieldInput = this.setupInput();
  }

  setupInput() {
    return (field) => {
      return (
        <Phone {...field.input} />
      );
    };
  }

  render() {
	return (<Field name={props.name} component={this.fieldInput} />)
  }
}

This made it work on inputing. I could properly select a country,enter the phone number, and pass it down to API in proper format. So far so good.

Once i load the same redux-form, with initialValues passed in, Nothing is prepopulated in the input, no country and no number. I console logged field.input and it properly contains all the events (onChange, onBlur, etc), and has a value property that is a valid number (+441632960293). All other inputs that i have are properly populated.

React version: 15.6.1 Redux version: 5.0.5 Redux-form version: 6.8.0

Edit: Looks like the country is properly preselected, but the number input is empty.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
catamphetaminecommented, Jul 25, 2018

image

1reaction
catamphetaminecommented, Jul 25, 2018

So, the reason onChange wasn’t passed to the component is that <Field/> doesn’t pass it and instead it passes only input: { value, onChange } and meta: { ... } objects as properties. https://redux-form.com/6.0.0-rc.4/docs/api/field.md/ Therefore, you’ll have to wrap this component in a special ReduxFormPhoneInput wrapper like this:

function ReduxFormPhoneInput({ input: { value, onChange }, meta }) {
  return (
    <Phone value={value} onChange={onChange}/>
  )
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Redux form - Initial values not loading in the form
I have a rather long form which is used as an edit form for changing a client details. The form loads but does...
Read more >
React JS Redux Form with Initial value, Validations and Props ...
In this article we have mention steps to develop code for React Redux Form with Initial Value, Validations and Props handling.
Read more >
Field - Redux Form
The function is given the fields current value, all other form values, and the props passed to the form. If the field does...
Read more >
How To Add Initial Values into Your Redux Form - LinkedIn
Tip: If you want to easily see if your values were loaded properly, in each field set a placeholder attribute. Note that placeholder...
Read more >
React + Redux Form — Two ways to add initial values
My work projects rely heavily on the use of Redux Form (paired with React). A problem I struggled with was trying to figure...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found