javascript - 在反应jsx中的一个元素中实现一组对象作为属性
问题描述
我有一个组件,我想将一个未知的对象数组(长度未知)传递给它,并将其显示为内部输入标记中键和值格式的属性。
<Input
type="text"
label = {"length" + '(' + this.state.package_length_unit + ')'}
placeholder = "length"
defaultValue = {this.state.package_length ? this.state.package_length : ''}
onInputChange = { this.onInputChange }
attrs = {[{name: 'id',value: 'package_length'},{name: 'apikey',value: 'package_length'}]}
/>
在组件内部我有这样的东西
<input
type = {props.type}
ref = {this.inputRef}
className = {inputClassName}
disabled = {props.disabled ? 'disabled' : ''}
defaultValue = {props.defaultValue ? props.defaultValue : ''}
onChange = {props.onInputChange ? props.onInputChange : e => {}}
onKeyUp = {this.handleTyping}
placeholder = {
props.placeholder ? props.placeholder : ''
}
{...props.attrs} // ??
// ********* here I want to have ==>
// id = 'package_length'
// apikey = 'package_length'
// ********* printed
/>
有没有办法做到这一点?提前致谢。
解决方案
{
...props.attrs.reduce((prev, curr) => {
prev[curr.name] = curr.value;
return prev;
}, {})
}
推荐阅读
- python - 模型表格无法保存在数据库中
- database - 规范化数据库中不同类型的开放和关闭时间
- json - 在 Android Studio 中解析 JSON 字符串
- authentication - 无法从 auth0 获取访问令牌
- c - 为什么位移在 C 中是这样写的?
- python - 如何使用熊猫迭代列?
- php - 如何在不覆盖 checkAccess 和 hasAccess 方法的情况下控制对操作的访问
- azure-devops - Azure Pipelines - 重新创建历史构建
- ios - IONIC Push Notification - 如何检测为什么没有在 IOS 上传递通知?
- google-colaboratory - ImportError: cnot import - 'kneeLocator' from 'kneed' (/usr/local/lib/python3.7/dist-packages/kneed/__init__.py)