1

I have this TextField element. I want to conditionally add either value={x} or defaultValue={x}

this will work (without any conditionals)

<TextField
    id="pickupName"
    className="order-form-input"
    onBlur={this.fieldChange('name')}
    ref="pickupName"
    defaultValue={(order.pickup || {}).name || ''}
    // hintText="Pickup Contact Name"
    floatingLabelFixed={true}
    floatingLabelText="Contact Name"
/>

however, if I try to add conditionals (of course it will fail):

<TextField
    id="pickupName"
    className="order-form-input"
    onBlur={this.fieldChange('name')}
    ref="pickupName"
      {true ? defaultValue={(order.pickup || {}).name || ''} :
                            value={(order.pickup || {}).name || ''}}
    // hintText="Pickup Contact Name"
    floatingLabelFixed={true}
    floatingLabelText="Contact Name"
/>

is there a way to have the defaultValue or value properties point to functions instead of static strings? Given my need, I have to conditionally use either value or defaultValue because they behave quite differently.

2
  • Maybe you should just use value. Commented Dec 13, 2016 at 9:00
  • yes but if I use value, then I have to use onChange, which is overkill, updating for every keystroke.. Commented Dec 13, 2016 at 9:11

1 Answer 1

1

If you are using es6 you parse all remaining arguments in the end of component declaration using {...args} operator.

Example:

<TextField
    id="pickupName"
    className="order-form-input"
    onBlur={this.fieldChange('name')}
    ref="pickupName"
    floatingLabelFixed={true}
    floatingLabelText="Contact Name",
    {...extra_args}
    />

I hope it will work.

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

1 Comment

thanks! can you show an example of what extra_args might look like? array or plain object?

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.