📌  相关文章
📜  仅在收到 API 响应后才呈现反应组件 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:29.758000             🧑  作者: Mango

代码示例1
let component = () => {

let [chartData , setChartData ] = useState();
// an async API call that loads data into chartData state after successful response

return(
    {chartData && 
    )
}


/* initially, chartData state will be undefined, thus the code block inside the 
return will evaluate to undefined ie. it will not be rendered. 
once the API response is received and charData will be assigned some real data, the component will rerender. 
this time, the code block will not evaluate to undefined. ie. the code block will render
*/

// for a more detailed discussion, read the below article 
https://daveceddia.com/watch-out-for-undefined-state/