javascript - React 组件利用率

标签 javascript reactjs ecmascript-6

我尝试在我的文件中使用以下组件,但无法按照组件的要求格式化选项。这是新的...任何有关如何修复我的代码的想法将不胜感激。 组件下面的 Field 是我尝试使用它的方式。

/**
* Single select dropdown component
* - Uses an array passed to options prop
* - 'options' Array must have 'value' (what is submitted) & 'text' 
(what is displayed)
*/
export class SingleSelect extends React.Component {
render () {
    const {
        input,
        label,
        options,
        required,
        placeholder,
        meta: { error, warning, touched },
        ...props
    } = this.props;

    let message;
    const validationState = touched && ( error && 'error' ) || ( warning && 'warning' ) || null;

    if ( touched && ( error || warning ) ) {
        message = <span className="help-block">{ error || warning }</span>;
    }

    let placeholderText = 'Select an option';
    if ( placeholder ) {
        placeholderText = placeholder;
    }

    const asterisk = required && (<span className="form-field-required" />) || null;

    return (
        <FormGroup validationState={ validationState }>
            <ControlLabel>
                { label }
                { asterisk }
            </ControlLabel>

            <FormControl {...{
                ...input,
                componentClass: 'select',
                placeholder: placeholderText,
                ...props
            }} >
                <option value="">{ placeholderText }</option>
                {
                    options.map((option, index) => (
                        <option key={index} value={option.value}>
                            {option.text}
                        </option>
                    ))
                }
            </FormControl>
            { message }
            <FormControl.Feedback />
        </FormGroup>
    );
}
}

<Field
 name="type"
 label="Type"
 component={fields.SingleSelect}
  options={
     text=[Media File, External File]
     value=type
  }
  required
  placeholder
  validate={[validators.required]}
 />

最佳答案

您在传递的对象周围需要额外的 {}:

<Field
  name="type"
  label="Type"
  component={fields.SingleSelect}
  options={[{
     text: 'Media File'
     value: type
  },{
     text: 'External File'
     value: type
  }]}
  required
  placeholder
  validate={[validators.required]}
/>

在 JSX 中,第一个 {} 表示里面是需要运行的 JavaScript。因此,您可以考虑忽略第一个 {}

关于javascript - React 组件利用率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48568608/

相关文章:

javascript - 哪个有更好更流畅的 DOM 操作 API? Chrome 还是火狐?

javascript - Highcharts - 防止在隐藏系列时重新计算值

javascript - 有没有办法将 [[Promise Result]] 保存为变量?

javascript - 处理数组时使用扩展语法 (...) 和 push.apply 之间的区别

javascript - 如何使用 Underscore.js 转换/混淆对象中的每个值

javascript - 获取两个最接近数组中值的数字

javascript - 在react组件中使用箭头函数绑定(bind)函数,而不是在构造函数中绑定(bind)

javascript - 单击时显示数组中的最后一个索引(在持续更新的数组中)

javascript - 我的函数计算总价格值,但总是落后一步?

javascript - ES6 按数组中的对象属性求和