简体   繁体   English

有什么办法可以用状态立即响应吗?

[英]is there any way to usestate respond immediately?

I was wanting to get value from the my server of the node with the usestate, but it returns the empty array value first and then pops up the value of my axios我想从我的节点服务器获取值,但它首先返回空数组值,然后弹出我的 axios 的值

has any way of the value of the axios appears first than that of the empty array? axios 的值是否比空数组的值先出现?

my code:我的代码:

 const [valTop, valTopstate] = useState([]) async function MusicTop() { axios("http://localhost:3001/").then(res=>{ valTopstate(res.data.tracks.data) }) } useEffect(()=>{ MusicTop() }, [valTop])

Your code is fine but UseEffect is executed only after the component is rendered, so you can apply a logic that is the array is empty then show the loader and if not show the array values, and if this is your child component what you can do is that when this child component is going to be executed then you can first fetch the required array value by calling a function in the parent component and pass the values as props in the child component.您的代码很好,但 UseEffect 仅在呈现组件后执行,因此您可以应用数组为空的逻辑,然后显示加载程序,如果不显示数组值,如果这是您的子组件,您可以做什么就是当这个子组件要被执行时,你可以首先通过在父组件中调用 function 来获取所需的数组值,并将这些值作为 props 传递到子组件中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM