我有一个<Day/>
组件,它<Calendar/>
自然是组件的子组件;
<Day/>
有一个dayInfo
道具。此时,我正在尝试转换通过event.target.computedName
单击日历获得的数据并将其传递给 day 组件,如下所示:<Day dayInfo={event.target.computedName}/>
event.target.computedName === 'April 9, 2020'
从那里我想将它转换为一个JSON
对象,然后将其推送到日组件中的状态/数组:
var [dayCollection, setDayInCollection] = useState([]);
So April 9, 2020 becomes:
dayCollection = [
"April-9-2020" : {} // a JSON object awaits some data about the day!!!
]
所以这是我的 Day 组件:
export default function Day({ dayInfo }) {
var [dayCollection, setDayInCollection] = useState([]);
useEffect(() => {
console.log('dayInfo ', dayInfo);
if (!dayInfo in dayCollection) {
setDayInCollection(
dayCollection.concat(
JSON.parse(
JSON.stringify(
dayInfo
.split(' ')
.map((item) => {
if (item.indexOf(',')) return item.replace(/,/g, '');
})
.join('-')
)
)
)
);
} else {
console.log('do nothing');
}
}, [dayCollection]);
console.log('dayCollection ', dayCollection);
/* rest omited for brevity */
}
我相信我已经将“2020 年 4 月 9 日”转换为“2020 年 4 月 9 日”,但dayCollection
/state 的检查不断产生[]
那么如何检查该JSON
对象是否存在于 Day 组件的状态中,如果它没有生成JSON
对象并将其连接到状态中,则添加它?
提前致谢!