reactjs - 如何在文本字段值中映射具有特定 ID 的日期?
问题描述
我有一个包含名为dates
的对象的数组,例如数组中的一个对象:{id: 9898, date: 10/06/2020}
。
在数组中,会有许多具有相同 id 的对象,我想在 中映射具有相同 id 的日期TextField
,请参见下面的代码。我该如何做到这一点?
- 任何帮助表示赞赏,谢谢!
import React, { useEffect, useState } from 'react';
import { useForm } from 'react-hook-form';
import { TextField, Box, Button, Tooltip } from '@material-ui/core';
return(
<div>
{dates.map(dateValue => (
<div>
<TextField label="Date" value={dateValue.date} fullWidth />
<Box mt="1.5rem" />
</div>
))}
<div>
<TextField
name="date"
id="date"
label="New date"
inputRef={register}
fullWidth />
</div>
<Box mt="1.5rem" />
</div>
);
解决方案
有一个使用 uuid的选项,例如react-uuid
UUID(通用唯一标识符)是一个 128 位数字,用于唯一标识 Internet 上的某个对象或实体。
import uuid from "react-uuid";
const udates = dates.map(x => ({ ...x, uuid: uuid() }));
<div key={dateValue.uuid} ... />
你也可以使用uuid
import {v5 as uuid} from "uuid";
他们有打字稿支持@types/uuid
推荐阅读
- sql - 如何将日期“20200901”转换为“01-Sep-20”
- r - 在 Nodejs 中加密时间戳并在 R 闪亮应用程序中解密
- google-chrome - 如何强制 Chrome 在没有命令行或快捷方式的情况下始终作为应用程序打开
- laravel - 当产品 is_featured 列状态都等于零时,我想使用 laravel 隐藏 html 代码
- c# - Aforge 视频 - 相机被其他设备使用
- node.js - 启动 vscode 调试传递节点环境变量以与 npm 调试模块一起使用
- flutter - “FirebaseFirestore”类型不是“Firestore”类型的子类型
- python - sqlalchemy 查询以获取外键(父)组合的最新数据
- r - .getReactiveEnvironment()$currentContext() 中的 R studio / R 脚本错误:
- java - 数组*不抛出越界异常