该组件显示GIF搜索的结果.为了添加到Collection 夹,我使用redux. 如果图标被添加到Collection 夹,我会更改图标的 colored颜色 .如何比较数组ID结果和Collection 夹?

import React, {FC, useState} from 'react';
import {IFavoriteGif, IFavoriteGifs, ISearchGifs} from "../../types/types";
import {useDispatch, useSelector} from "react-redux";
import { Col, Row} from 'antd';
import {HeartTwoTone} from '@ant-design/icons';

interface IGifsResultProps {
    results: ISearchGifs[];
}

const GifsResult: FC<IGifsResultProps> = ({results}) => {

    const dispatch = useDispatch();
    const favorites = useSelector((state: IFavoriteGifs) => state.favorites);

    const addFavorites = (id: string, src: string) => {
        const data = {
            id: id,
            src: src,
        }
        dispatch({type: "ADD_FAVORITE_GIFS", payload: data})
    }

    return (
        <Row>
            {results.map((result, i) =>
                <Col span={8} key={result.id}>
                    {//if ids matches
                            ? <HeartTwoTone twoToneColor="red"/>
                            : <HeartTwoTone twoToneColor="gray"/>
                    }
                    <img
                        src={result.images.fixed_height.url}
                        onClick={()=> addFavorites(result.id, result.images.fixed_height.url)}
                    />
                </Col>
            )}
        </Row>
    );
};

export default GifsResult;

推荐答案

就像这样:

<HeartTwoTone twoToneColor={favorites.some(f => f.id === result.id) ? 'red' : 'grey'}/>

Javascript相关问答推荐

IOS(React Native)中未找到模块SquareReaderSDK

Vue-Router渲染组件但不更改网址

JavaScript Date对象在UTC中设置为午夜时显示不正确的日期

如何使用Paged.js仅渲染特定页面

如何使用JavaScript用等效的功能性HTML替换标记URL格式?

微软Edge Select 间隙鼠标退出问题

Angular中计算信号和getter的区别

Redux查询多个数据库Api reducerPath&

在vercel throws上部署带有gunjs的sveltekit应用无法找到模块./' lib/文本编码'

我可以从React中的出口从主布局调用一个处理程序函数吗?

嵌套异步JavaScript(微任务和macrotask队列)

如何从一个列表中创建一个2列的表?

在我的html表单中的用户输入没有被传送到我的google表单中

如何将Openjphjs与next.js一起使用?

我可以使用使用node.js创建的本地主机来存储我网站上提交的所有数据吗?没有SQL或任何数据库.只有HTML语言

rxjs插入延迟数据

如何使用JavaScript拆分带空格的单词

用于在路径之间移动图像的查询

在css中放置所需 colored颜色 以填充图像的透明区域

无法避免UV:flat的插值:非法使用保留字"