web-dev-qa-db-fra.com

Redux form defaultValue

Comment définir defaultValue pour entrer le composant?

<Field name={`${prize}.rank`} defaultValue={index} component={Input} type='text'/>

J'ai essayé comme ci-dessus mais mes champs sont vides. J'essaie de créer fieldArray (formulaires dynamiques):

{fields.map((prize, index) =>
    <div key={index} className="fieldArray-container relative border-bottom" style={{paddingTop: 35}}>
        <small className="fieldArray-title marginBottom20">Prize {index + 1}
            <button
                type="button"
                title="Remove prize"
                className="btn btn-link absolute-link right"
                onClick={() => fields.remove(index)}>Delete</button>
        </small>
        <div className="row">
            <div className="col-xs-12 col-sm-6">
                <Field name={`${prize}.rank`}  defaultValue={index} component={Input} type='text'/>
                <Field name={`${prize}.prizeId`} defaultValue={index} component={Input} type='text'/>
                <Field
                    name={`${prize}.name`}
                    type="text"
                    component={Input}
                    label='Prize Name'/>
            </div>
            <div className="col-xs-12 col-sm-6">
                <Field
                    name={`${prize}.url`}
                    type="text"
                    component={Input}
                    label="Prize URL"/>
            </div>
            <div className="col-xs-12">
                <Field
                    name={`${prize}.description`}
                    type="text"
                    component={Input}
                    label="Prize Description" />
            </div>
        </div>
    </div>
)}
16

Sur les formulaires redux, vous pouvez appeler initialize() avec un objet de valeurs comme ceci:

class MyForm extends Component {
  componentWillMount () {
    this.props.initialize({ name: 'your name' });
  }

  //if your data can be updated
  componentWillReceiveProps (nextProps) {
    if (/* nextProps changed in a way to reset default values */) {
      this.props.destroy();
      this.props.initialize({…});
    }
  }

  render () {
    return (
      <form>
       <Field name="name" component="…" />
      </form>
    );
  }
}

export default reduxForm({})(MyForm);

De cette façon, vous pouvez mettre à jour les valeurs par défaut encore et encore, mais si vous avez juste besoin de le faire la première fois, vous pouvez:

export default reduxForm({values: {…}})(MyForm);
31
philipp

Ce jsfiddle a un exemple

https://jsfiddle.net/bmv437/75rh036o/

const renderMembers = ({fields}) => (
    <div>
    <h2>Members</h2>
    <button onClick={() => fields.Push({})}>add</button><br />
      {fields.map((field, idx) => (
        <div className='member' key={idx}>
        First Name
        <Field name={`${field}.firstName`} component='input' type='text' /><br />
        Last Name
        <Field name={`${field}.lastName`} component='input' type='text' /><br />
            <button onClick={() => fields.remove(idx)}>remove</button><br />
        </div>
    ))}
    </div>
)

const Form = () => (
  <FieldArray name='members' component={renderMembers} />
)

const MyForm = reduxForm({
    form: 'foo',
  initialValues: {
    members: [
        {
        firstName: "myFirstName"
      }
    ]
  }
})(Form)
8
radix

c'est ma mise en œuvre en utilisant un HoC

import { Component } from 'react'
import {
  change,
} from 'redux-form'

class ReduxFormInputContainer extends Component{
  componentDidMount(){
    const {
      initialValue,
      meta,
    } = this.props
    if(initialValue === undefined || meta.initial !== undefined || meta.dirty) return
    const {
      meta: { form, dispatch },
      input: { name },
    } = this.props
    dispatch(change(form, name, initialValue))
  }
  render(){
    const {
      initialValue,
      component: Compo,
      ...fieldProps
    } = this.props
    return <Compo {...fieldProps} />
  }
}

function reduxFormInputContainer(component){
  return function(props){
    return <ReduxFormInputContainer {...props} component={component} />
  }
}

export default reduxFormInputContainer

puis par exemple:

import reduxFormInputContainer from 'app/lib/reduxFormInputContainer'

InputNumericWidget = reduxFormInputContainer(InputNumericWidget)

class InputNumeric extends Component{
  render(){
    const props = this.props
    return (
      <Field component={InputNumericWidget} {...props} />
    )
  }
}
1
Jo-Go