1

I have this Field that renders an Input component 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"
        component={RenderInput}
        onChange={(event) => {
          handleSubmit(currentValues => this.debounceSubmitProduction({
            ...currentValues,
            hours: event.target.value,
          }))();
        }}
      />

This is the RenderInput component:

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. How can I pass the property value from <Field /> to RenderInput?

3 Answers 3

6

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

With an uncontrolled component, 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.

Sign up to request clarification or add additional context in comments.

1 Comment

To piggyback on this answer: Only use the defaultValue attribute if you don't want the user to control the state of the input. If they're going to be making changes to the input use the value attribute instead. This will make the input controlled and will be updated via a setState function that you pass from your parent component to the Field component.
1

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"
        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

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

3 Comments

I want the default value to be empty string, but when I pass the props with a default value, to show that value. I tried this but it didn't work :(
The error is: Uncaught (in promise) TypeError: Cannot read property 'replaceChild' of null
You can setup default values on component with static defaultProps = {} and on stateless components (which you have on render input) same as I stated above, RenderInput.defaultProps = { input: { value: 100 } } unless you will provide full example on stackblitz.com the uncaught error is probably unrelated to the question which you've asked
1
  <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>
);

3 Comments

This is what I tried before, with value and with defaultValue but none of them worked :(
Well Did you get any error in console? as well answer updated
The errror is: Uncaught (in promise) TypeError: Cannot read property 'replaceChild' of null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.