简体   繁体   English

为什么这个 console.logconsole.log(data.lyrics) 在 react.js 中给我 undefined?

[英]why does this console.logconsole.log(data.lyrics) gives me undefined in react.js?

Would you please tell me what I'm doing wrong here?你能告诉我我在这里做错了什么吗?

import { useState,useEffect } from 'react';

const useFetchData = (url) => {

    const [data,setData]=useState( [] );   

        useEffect(()=>{
            fetch(url,{
                crossDomain:true, 
                headers:{
                'Content-Type': 'application/json',}
            })
            .then(res =>{
                return res.json();
            })
            .then(dataa =>{
                /* setData(dataa.message.body.track_list.map(trc =>{
                    return [[trc.track.track_name],[trc.track.artist_name],[trc.track.album_name]];
                }));*/
                setData(dataa.message.body);
                
                /* data.message.body.track_list.trac.track.map((trac)=>{
                })
                console.log(data);
                console.log(dataa.message.body);*/
            }).catch(err => console.log(err));
            console.log(data.lyrics);
        },[])
    return data;
}
 
export default useFetchData;

Because you're calling data from inside the useEffect hook, you're only getting the value of data at the point where the hook was executed.因为您是从useEffect挂钩内部调用data ,所以您只能在执行挂钩时获取data的值。 setData doesn't update the data variable immediately, but updates the state and triggers a rerender of the component. setData不会立即更新data变量,而是更新 state 并触发组件的重新渲染。

If you move the console.log(data.lyrics);如果你移动了console.log(data.lyrics); out of the hook, you'll see that once the component is rerendered its value should be correct.摆脱困境,你会看到一旦组件被重新渲染,它的值应该是正确的。

暂无
暂无

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

相关问题 为什么 React.js axios 数据显示在控制台而不显示在屏幕上? - Why does React.js axios data show in console but not on the screen? 为什么React.js有时不让我使用`debugger` - Why does React.js sometimes not let me use `debugger` 为什么我的 setTimeOut 在 Mobx 中给了我代理对象,react.js - why my setTimeOut gives me Proxy Object in Mobx, react.js 为什么 React.js 中的 1 个 undefined 属性 - Why 1 property of undefined in the React.js 为什么我无法获取 AJAX 中的表数据? 每次我从表中获取 req_id 时,它都会在 Console.log 中给我未定义的值 - Why i am not able to fetch table data in AJAX ? Everytime i fetch req_id from table it's gives me undefined value in Console.log React.js 提交按钮在控制台日志上不起作用? - React.js submit button didn't work on Console Log? JS函数给我控制台中未定义的内容,但我期望有一个数字 - JS Function Gives Me Undefined in Console but I'm Expecting a Number React.js - 未捕获的类型错误:无法读取未定义的属性(硬编码数据时不会发生) - React.js - Uncaught TypeError: Cannot read properties of undefined (does not happen when data is hardcoded) 为什么当我尝试在 react.js 中获取图像的高度和宽度时它返回未定义 - Why does it return undefined when I try to get the height and width of an image in react.js node.js console.log抛出了我未定义的内容 - node.js console.log throws me undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM