最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass a default value to an input in React (using props) - Stack Overflow

programmeradmin0浏览0评论

I have this Field that renders an Input ponent RenderInput. I need to pass a default value to it, something like value="100".

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

This is the RenderInput ponent:

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 ponent, it works fine, but If I try to pass that value as a props to the RenderInput ponent, it doesn't work. How can I pass the property value from <Field /> to RenderInput?

I have this Field that renders an Input ponent RenderInput. I need to pass a default value to it, something like value="100".

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

This is the RenderInput ponent:

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 ponent, it works fine, but If I try to pass that value as a props to the RenderInput ponent, it doesn't work. How can I pass the property value from <Field /> to RenderInput?

Share Improve this question asked Apr 25, 2018 at 18:56 Lizz ParodyLizz Parody 1,76513 gold badges31 silver badges50 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

https://reactjs/docs/uncontrolled-ponents.html#default-values

With an uncontrolled ponent, you often want React to specify the initial value, but leave subsequent updates uncontrolled. To handle this case, you can specify a defaultValue attribute instead of value.

Well you have several options and your question is wrong as default value is done via defaultProps but what you can do is

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

please pay attention how it's destructured inside 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.

If you want to have default value, you can just add static to 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"
    ponent={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>
);
发布评论

评论列表(0)

  1. 暂无评论