Javascript Redux 表单默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43631435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:03:33  来源:igfitidea点击:

Redux form defaultValue

javascriptreactjsreduxredux-form

提问by Андрей Гузюк

How to set defaultValueto input component?

如何设置defaultValue为输入组件?

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

I tried like above but my fields are empty. I'm trying to create fieldArray(dynamic forms):

我像上面那样试过,但我的字段是空的。我正在尝试创建fieldArray(动态表单):

{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>
)}

回答by philipp

On redux forms you can call initialize()with an object of values like so:

在 redux 表单上,您可以initialize()使用这样的值对象调用:

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

This way you can update the default values over and over again, but if you just need to do it at the first time you can:

通过这种方式,您可以一遍又一遍地更新默认值,但如果您只需要在第一次执行此操作,您可以:

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

回答by radix

This jsfiddle has an example

这个jsfiddle有一个例子

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

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)

回答by Jo-Go

this is my implementation using a HoC

这是我使用 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

and then for exemple:

然后例如:

import reduxFormInputContainer from 'app/lib/reduxFormInputContainer'

InputNumericWidget = reduxFormInputContainer(InputNumericWidget)

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