简体   繁体   English

React typescript 属性“地图”在“用户”类型上不存在

[英]React typescript Property 'map' does not exist on type 'User'

I am trying to use react with typescript.我正在尝试对 typescript 做出反应。 I have initialized useState with an object but can't use map function with that object.我已经用 object 初始化了 useState 但不能使用 map function 和 ZA8CFDE6331BD59EB2ACZ66F14Z

Here is the error I am getting这是我得到的错误

Property 'map' does not exist on type 'User' “用户”类型上不存在属性“地图”

Here is the code.这是代码。

Thank you in advance先感谢您

    interface User  {
        name : string,
        email : string,
        stream_key : string,
    
    }
    
    const App = () => {
    const [liveStreams, setLiveStreams] = useState<User>({
            name : '',
            email : '',
            stream_key : ''
        })
    
    // setting livestreams
    const getStreamsInfo = (live_streams) => {
        axios.get('http://192.168.43.147:4000/streams/info', {
        }).then(res => {
            console.log(res.data)
            setLiveStreams({
                name: res.data.name,
                email: res.data.email,
                stream_key: res.data.stream_key
            })
        });
    }
    
    return (
    {liveStreams.map(data => <Text>{data.email}</Text>)}    
)

You only have a single User object, not an array of them.您只有一个User object,而不是它们的数组。 Either:任何一个:

  1. Just use the object (and ideally use the singular rather than the plural for the name of the state variable), or只需使用 object (最好使用单数而不是复数作为 state 变量的名称),或

  2. Use an array of objects instead.改为使用对象数组。

With #1, for instance, if you used the name liveStream , it would look like this:例如,对于 #1,如果您使用名称liveStream ,它将如下所示:

return <Text>{liveStream.email}</Text>;

Given that you've said鉴于你说过

Actually res.data contains data of multiple users.实际上 res.data 包含多个用户的数据。 How to use array of objects using typescript?如何使用 typescript 使用对象数组? Sorry for newbie questio对不起新手问题

in a comment, it looks like you want #2, which means:在评论中,看起来你想要#2,这意味着:

  1. Make your initial data an empty array.使您的初始数据成为一个空数组。

     const [liveStreams, setLiveStreams] = useState<User[]>([]); // −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^^−−^^
  2. When you receive the list, use the whole list:当您收到列表时,请使用整个列表:

     const getStreamsInfo = (live_streams) => { axios.get('http://192.168.43.147:4000/streams/info', { }).then(res => { // To *replace* state with the list in `res.data`: setLiveStreams(res.data); // >>OR<< to **add to** the current state, appending the list from `res.data`: setLiveStreams(streams => [...streams, ...res.data]); }); }
  3. Add key attributes to your Text elements, since they're entries in an array.为您的Text元素添加key属性,因为它们是数组中的条目。

In your case, the actual solution will be to use a type guard to check that you have an array before attempting to use the map method.在您的情况下,实际的解决方案是在尝试使用 map 方法之前使用类型保护来检查您是否有一个数组。

if (liveStreams instanceof Array) {
    liveStreams.map(data => <Text>{data.email}</Text>);
}
   interface User  {
        name : string,
        email : string,
        stream_key : string,
    
    }
    
    const App = () => {
    const [liveStreams, setLiveStreams] = useState<User[]>([{
            name : '',
            email : '',
            stream_key : ''
        }])
    
    // setting livestreams
    const getStreamsInfo = (live_streams) => {
        axios.get('http://192.168.43.147:4000/streams/info', {
        }).then(res => {
            console.log(res.data)
           // is the `res.data` an Array Object?
           // or u may just do like that 
           /*
            * const { data } = res
            * // if `data` type is user[]
            * setLiveStreams(data)
           */
            setLiveStreams([{
                name: res.data.name,
                email: res.data.email,
                stream_key: res.data.stream_key
            }])
        });
    }
    
    return (
    {liveStreams.map(data => <Text>{data.email}</Text>)}    
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Typescript 中的类型不存在属性 - property does not exist on type in React Typescript React Custom Hook with Context and Typescript:属性“map”在类型“Option”上不存在 - React Custom Hook with Context and Typescript: Property 'map' does not exist on type 'Option' 类型{}不存在Typescript属性 - Typescript property does not exist on type {} 类型 [] 打字稿上不存在属性 - property does not exist on type [] typescript 属性在类型上不存在-TypeScript - Property does not exist on type - TypeScript 类型/ IntrinsicAttributes和IntrinsicClassAttributes上不存在React Typescript属性 - React Typescript property does not exist on type / IntrinsicAttributes & IntrinsicClassAttributes 与 Typescript 反应:“历史”类型上不存在“推送”属性 - React with Typescript: Property 'push' does not exist on type 'History' React Typescript:属性“body”不存在类型“DefaultTheme” - React Typescript: property 'body' does not exist type 'DefaultTheme' React TypeScript 和 ForwardRef - 类型“IntrinsicAttributes”上不存在属性“ref” - React TypeScript & ForwardRef - Property 'ref' does not exist on type 'IntrinsicAttributes “类型上不存在属性'名称'”反应网站中的 typescript 问题 - "Property 'name' does not exist on type" typescript problem in react website
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM