我的 src 有问题。我需要使用 API 中的 ID 号才能在我的网站上显示图像。当我这样做时,它向我显示终端中的错误'类型'数字'不可分配给类型'字符串|未定义'.'
import React from 'react';
import styled from 'styled-components';
import {Link} from 'react-router-dom';
import { useSelector } from 'react-redux';
import { IState } from '../../reducers';
import { IUsersPhotos } from '../../reducers/usersPhotos';
const ProfileImg = styled.img`
margin: 0 auto;
display: block;
cursor: pointer;
`
const ProfileImage = () => {
// const { usersList } = useSelector<IState, IUsersReducer>(globalState => globalState.users);
const { usersPhotos } = useSelector<IState, IUsersPhotos>(globalState => globalState.photos);
return(
<Link to="/profilePage" style={{textDecoration: 'none'}}>
<ProfileImg src= {usersPhotos?.[0]?.id} />
</Link>
)
}
export default ProfileImage;
答案 0 :(得分:0)
听起来您应该更改类型以表示数据类型 number
?
没有看到你的类型很难说。应该这样做:
type IUsersPhotos {
data: {
id?: number | string
}
}