【问题标题】:defaultValue is not filled in textarea when used with react-final-form与 react-final-form 一起使用时,textarea 中未填充 defaultValue
【发布时间】:2018-09-11 03:09:17
【问题描述】:

我无法将 defaultValue 设置为文本区域,我正在使用 react-final-form 并将 TextareaField 包装在 react-final-form 的 Field 元素中

这是我的代码:

class App extends React.Component {
  submitForm(values) {
   .....
  }

 renderForm({ handleSubmit }) {
  return (
   <form
    onSubmit={handleSubmit}
   >
    <Field
      name="description"
    >
      {({ input, meta }) => (
        <TextareaField
          label='Description'
          error={meta.error}
          isInvalid={meta.submitFailed && !meta.valid}
          inputAttrs={{
            placeholder: 'Desc..',
          }}
          defaultValue='my text'
          onChange={(e) => { input.onChange(e.target.value); }}
          value={input.value}
          required
        />
      )}
    </Field>
    <Button text="Submit" type={Button.Opts.Types.SUBMIT} />
  </form>
 );
}
render() {
  return (
    <Form
      onSubmit={this.submitForm}
      render={this.renderForm}
      validate={(values) => {
        const errors = {};
        if (!values.description) {
          errors.description = 'Required';
        }
        return errors;
      }}
    />
   );
 }
}

卡住了,我错过了哪里??

【问题讨论】:

    标签: reactjs textarea react-final-form


    【解决方案1】:

    如果您希望 my text 成为字段中的初始值,则需要将 initialValues={{ description: 'my text' }} 传递给 Form 组件。

    【讨论】:

      猜你喜欢
      • 2021-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      相关资源
      最近更新 更多