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.

Setting Initial value of field.

See original GitHub issue

Hi, I current having a huge problem with setting an initial value to a field and can’t work out why this is causing me such a problem. I am currently days old into learning redux and redux-form so I am hoping its my inexperience.

So to put it simply, I am trying to set project.name to its initial value of the “name” Field when editing a project but it shows no value. This even happens when I hard code the value in. e.g.

<Field name="name"
           value="tim"
           type="text"
           component={renderField}
           label="Project Name"
           validate={[ required ]}/>

HTML Output: alt text

Full File:

import React, {Component, PropTypes} from 'react';
import {connect} from 'react-redux';
import {createProject, updateProject} from '../actions/projectActionCreators';
import {bindActionCreators} from 'redux';
import { Field, reduxForm, SubmissionError, stopAsyncValidation } from 'redux-form';
import {ErrorsList} from '../components/ErrorsList';
import {Link} from 'react-router';

const required = value => value ? undefined : 'Required';
class EditProject extends Component{
    constructor(props) {
        super(props);
    }
    static contextTypes = {
        router: PropTypes.object
    };
    onSubmit(props){
        props.workspace_id = this.props.workspace.id;
        this.setState({errors: null});
        const then = (response)=> {
            if (response.error){
                this.setState({errors: response.payload.response.data.errors});
            }else {
                this.context.router.push(`/projects/${response.payload.data.id}`);
            }
        };
        if (this.props.params.id){
            props.id = this.props.params.id;
            this.props.updateProject(props);
        } else {
            this.props.createProject(props).then(then);
        }

    }
    showServerErrors(){
        if (this.state && this.state.errors){
            return <ErrorsList errors={this.state.errors}/>
        }
    }
    render(){
        const { handleSubmit } = this.props;
        const project = this.props.params.id ? this.props.workspace.projects.find(p => p.id == this.props.params.id) : null;
        return(
            <div className="row">
                <div className="col-md-4"></div>
                <div className="col-md-4">
                    <h1 className="page-header">{project ? 'Edit' : 'Create' } Project</h1>
                    {this.showServerErrors()}
                    <form onSubmit={handleSubmit(this.onSubmit.bind(this))}>
                        <Field name="name"
                               value={project ? project.name : ''}
                               type="text"
                               component={renderField}
                               label="Project Name"
                               validate={[ required ]}/>
                        <button type="submit"  className="btn btn-primary" >Submit</button>
                        <Link to="/" className="btn btn-default" style={{marginLeft: 5}}>Cancel</Link>
                    </form>
                </div>
            </div>
        );
    }
}

const validate = values => {
    const errors = {};
    return errors;
}

const renderField = ({ input, label, type, meta: { touched, invalid ,error, warning } }) => {
    return (
        <div className={`form-group has-danger`}>
            <label className="form-control-label">{label}</label>
            <input className={`form-control form-control-danger`} {...input} placeholder={label} type={type}/>
            <div className="text-help text-danger">
                {touched ? error : ''}
            </div>
        </div>
    );
};

function mapStateToProps(state){
    return {
        workspace: state.workspace
    }
}

let formComponent = reduxForm({
    form: 'EditProjectForm',
    validate
})(EditProject);

export default connect(mapStateToProps, {createProject, updateProject})(formComponent);



Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7

github_iconTop GitHub Comments

41reactions
gustavohenkecommented, Jan 3, 2017

You need to use initialValues prop. For example, in your mapStateToProps function, include a key initialValues in your returned object with the value { name: 'blabla' } }, and your field will initialize with the value blabla 😮

Please note that this issue doesn’t belong to our GitHub issue tracker, so in the future we request you to ask at StackOverflow, using the redux-form tag.

This is because StackOverflow is better at keeping popular questions visible. The GitHub issue tracker is more suited for discussions related to bugs and improvements of Redux Form.

Thanks for using Redux Form!

23reactions
songshanslscommented, Jun 14, 2018

try initialize function componentWillMount () { this.props.initialize({ name: 'blabla' }) }

Read more comments on GitHub >

github_iconTop Results From Across the Web

<Field> initialValue / defaultValue prop · Issue #387 - GitHub
The current behavior is that I'm passing initial values to a Form component and a date Field inside the form has defaultValue={new Date()}...
Read more >
Initial value for form fields - Zoho Creator Help
The field property Initial Value enables you to set a value to a field when your users access your form to submit an...
Read more >
Setting an Initial Value for a Field - IBM
Double-click the element for which you want to set an initial value. · Click the Display tab. · In the initial value box,...
Read more >
Setting default value of a field - Jotform
Click Settings. Navigate to the Conditions tab. Create the new condition. Configure your conditions to set default values.
Read more >
Setting a default value for an Input element in React | bobbyhadz
To set a default value for an input element in React, pass the default value as a parameter to the `useState` hook for...
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