简体   繁体   English

如何将默认值传递给React中的输入(使用props)

[英]How to pass a default value to an input in React (using props)

I have this Field that renders an Input component RenderInput . 我有此字段呈现一个输入组件RenderInput I need to pass a default value to it, something like value="100" . 我需要将默认值传递给它,例如value="100"

      <Field
        name="hours"
        type="number"
        placeholder="Ingrese las horas para esta categoría"
        component={RenderInput}
        onChange={(event) => {
          handleSubmit(currentValues => this.debounceSubmitProduction({
            ...currentValues,
            hours: event.target.value,
          }))();
        }}
      />

This is the RenderInput component: 这是RenderInput组件:

const RenderInput = ({
  input,
  type,
  disabled,
  readOnly,
  placeholder,
  meta: { touched, error },
  onKeyDown,
  innerRef,
}) => (
  <div>
    <input
      {...input}
      type={type}
      disabled={disabled}
      readOnly={readOnly}
      placeholder={placeholder}
      className={`font-300 ${touched && error && 'has-error'}`}
      onKeyDown={onKeyDown}
      ref={innerRef}
    />
  </div>
);

If I put value="100" inside the RenderInput component, it works fine, but If I try to pass that value as a props to the RenderInput component, it doesn't work. 如果将value="100"放在RenderInput组件内,则可以正常工作,但是如果我尝试将该值作为道具传递给RenderInput组件,则它将无法正常工作。 How can I pass the property value from <Field /> to RenderInput? 如何将属性value<Field />传递给RenderInput?

https://reactjs.org/docs/uncontrolled-components.html#default-values https://reactjs.org/docs/un受控组件.html#default-values

With an uncontrolled component, you often want React to specify the initial value, but leave subsequent updates uncontrolled. 对于不受控制的组件,您通常希望React指定初始值,但使后续更新不受控制。 To handle this case, you can specify a defaultValue attribute instead of value . 要处理这种情况,可以指定defaultValue属性而不是value

Well you have several options and your question is wrong as default value is done via defaultProps but what you can do is 好吧,您有几个选择,您的问题是错误的,因为默认值是通过defaultProps完成的,但是您可以做的是

<Field
        name="hours"
        type="number"
        placeholder="Ingrese las horas para esta categoría"
        component={() => (<RenderInput input={{ value: 100 }}/> )}
        onChange={(event) => {
          handleSubmit(currentValues => this.debounceSubmitProduction({
            ...currentValues,
            hours: event.target.value,
          }))();
        }}
      />

please pay attention how it's destructured inside RenderInput 请注意它在RenderInput内部的结构

const RenderInput = ({
  input,
  type,
  disabled,
  readOnly,
  placeholder,
  meta: { touched, error },
  onKeyDown,
  innerRef,
}) => (

this means anything passed on input key will be destructured as prop to input, so if you need to send value you have to do input={{value: 100}} instead of value=100 this prop is not destructured. 这意味着在input键上传递的所有内容都将被分解为输入的道具,因此,如果您需要发送值,则必须执行input={{value: 100}}而不是value=100该道具不会被破坏。

If you want to have default value, you can just add static to RenderInput 如果要使用默认值,可以将静态添加到RenderInput

RenderInput.defaultProps = {
  input: { value: 100 }
}

EDIT 编辑

There is alsoclone function 还有克隆功能

const ComponentWithProps = React.CloneElement(RenderInput, { props })

or 要么

  <Field
    name="hours"
    type="number"
    someValue='100'
    placeholder="Ingrese las horas para esta categoría"
    component={RenderInput}
    onChange={(event) => {
      handleSubmit(currentValues => this.debounceSubmitProduction({
        ...currentValues,
        hours: event.target.value,
      }))();
    }}
  />

Then 然后

const RenderInput = ({
  input,
  type,
  disabled,
  readOnly,
  placeholder,
  meta: { touched, error },
  onKeyDown,
  innerRef,
  someValue
}) => (
  <div>
    <input
      {...input}
      type={type}
      disabled={disabled}
      readOnly={readOnly}
      placeholder={placeholder}
      className={`font-300 ${touched && error && 'has-error'}`}
      onKeyDown={onKeyDown}
      ref={innerRef}
      defaultValue={someValue}
    />
  </div>
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM