redux-form - FieldArray 的每个元素中有 2 个字段,但 fields.get(index) 无法获取第二个字段
问题描述
我在 FieldArray 的每个元素中有 2 个字段,
但我使用 fields.get(),
它不返回第二个字段。
在AvailableShopSelectFields
组件和isSelected
功能中,我打印为...
console.log('isSelected', fields.get(fields.length - 1));
日志将仅显示 'isSelect { no: 1 }(我预计它会记录 { no: 1, id: 'something that I selected' }
父子组件
let ParentComponent = props => {
return (
.
.
<FieldArray name="shopIds" component={AvailableShopSelectFields} />
.
.
)
}
// Child component
const AvailableShopSelectFields = props => {
const { fields } = props;
const [isLastAvailableShopNotSelected, setIsLastAvailableShopNotSelected] = useState(true);
const isSelected = (value) => {
const shopId = fields.get(fields.length - 1).id;
console.log('isSelected', shopId === value, shopId, value, fields.get(fields.length - 1));
!shopId ?
setIsLastAvailableShopNotSelected(true) :
setIsLastAvailableShopNotSelected(false);
};
return (
<>
{fields.map((shopFields, idx) => (
<Row key={idx}>
<Col className="left-panel" span={2}>
<Field name={`${shopFields}.no`} label="No." component={InputField} disabled />
</Col>
<Col className="center-panel" span={12}>
<Field name={`${shopFields}.id`}
label="Shop Name"
component={AvailableShopSelectField}
validate={[VALIDATION.required]}
input={{onChange: isSelected}}
/>
</Col>
<Col className="right-panel">
<Button style={{ borderRadius: '50%', height: 40, width: 40, padding: 0 }}
onClick={() => fields.remove(idx)}
type="reset"
>
-
</Button>
</Col>
</Row>
)
)}
<Row>
<Col align="center" span={16}>
<Button style={{ borderRadius: '50%', height: 40, width: 40, padding: 0 }}
type="reset"
onClick={() => fields.push({ no: fields.length + 1 })}
disabled={isLastAvailableShopNotSelected}
>
+
</Button>
</Col>
</Row>
</>
);
};
AvailableShopSelectField.js
const AvailableShopSelectField = ({ input, label, meta }) => {
const [availableShops, setAvailableShops] = useState([]);
const { error, touched } = meta;
const validate = {
validateStatus: '',
help: ''
};
if (touched && error) {
validate.validateStatus = 'error';
validate.help = error;
}
useEffect(() => {
(async () => await fetchAvailableShops())()
}, []);
const fetchAvailableShops = async () => {
const { data: { data } = {} } = (await API.getMemberCardAvailableShop());
setAvailableShops(data.shops);
};
return (
<FormItem
{...validate}
label={label}
>
<FormSelect
{...input}
onChange={value => input.onChange(value)}
showSearch
style={{ width: "100%" }}
optionFilterProp="children"
filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
>
{availableShops.map(shop => (
<Option key={shop.id} value={shop.id}>{shop.name}</Option>
))}
</FormSelect>
</FormItem>
);
};
如何获得第二个字段?
用户界面
Redux 状态(未更新)
解决方案
推荐阅读
- javascript - 找不到入口模块和 webpack 中的错误
- python - 如何在模态框上渲染 ModelForm
- arrays - 使用节点 js 中的两个数组创建嵌套 json
- mysql - MYSQL 几何函数多边形
- ios - 如何将大图像添加到 UISlider 背景或放置在滚动视图中?
- jupyter-lab - 如何更改 Jupyter Lab 中的默认代码格式化程序?
- docker - 如何使用 docker-compose up 执行一次性/不太频繁的命令
- time-and-attendance - 标记考勤时,考勤卡中的Kizala响应时间不准确
- java - 如何检查一个点元组是否与另一个元组连接?
- r - 将 Kera/Tensorflows 模型作为 R 中的包部署到生产环境