dom内容通过map遍历,useState应该怎么写才不显得冗余又可以正确对上每个input的内容和事件呢?一开始试了对象去堆,但发现语法没对,一直报错 /(ㄒoㄒ)/~~求指导
import React, { useState, Fragment } from 'react'
import { Input, Select, DatePicker, Col, Row } from 'antd'
const { Option } = Select;
const searchData = [
{
title: '商品名称',
type: 1,
id: 1
},
{
title: '商品品牌',
type: 1,
id: 2
},
{
title: '商品编号',
type: 1,
id: 3
},
{
title: '商家选择',
type: 2,
id: 5
},
{
title: '团队礼包',
type: 2,
id: 6
},
{
title: '创建日期',
type: 3,
id: 7
}
]
const GiftSearch = props => {
const obj = {
a1:[InputValue1, setInputValue1] = useState(''),
a2:[InputValue2, setInputValue2] = useState(''),
}
console.log(InputValue,'InputValue')
return (
<Row>
{
searchData.map(item => {
const { title, type, id } = item
return (
<Row key={id}>
<Col>{title}:</Col>
<Col>
{type === 1 && <Input value={obj[a+id]} onChange={(e)=>`${setInputValue+id(e.target.value)}`} />}
</Col>
</Row>
)
})
}
</Row>
)
}
export default GiftSearch
你需要 immer 这种 immutable 的库:https://github.com/immerjs/immer