最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Having trouble checking if an array has data or is empty in ReactJS - Stack Overflow

programmeradmin1浏览0评论

I have array in my state I set my state default value to an empty array []. After an API request loads I need to use loader until data is ready. So U use condition like:

(if array length  === 0  loader will show otherwise data will show)

but when the API returns empty data I want to show no data available.

What is best way to do that?

My code is below - how can I check the condition if the API returns an empty result?

this.state.offer.length == 0 ?  <Loader /> : <OfferList />

I have array in my state I set my state default value to an empty array []. After an API request loads I need to use loader until data is ready. So U use condition like:

(if array length  === 0  loader will show otherwise data will show)

but when the API returns empty data I want to show no data available.

What is best way to do that?

My code is below - how can I check the condition if the API returns an empty result?

this.state.offer.length == 0 ?  <Loader /> : <OfferList />
Share Improve this question edited Dec 15, 2017 at 11:08 Brett DeWoody 62.9k31 gold badges144 silver badges192 bronze badges asked Dec 15, 2017 at 11:03 Narayanan SNarayanan S 1271 gold badge5 silver badges14 bronze badges 2
  • 1 When you get the API result you should check if it's length is 0, then have a flag in state and set that like if responseFromAPI.length==0 this.setState({emptyAPIResponse: true}), then use that for conditional rendering – Jayce444 Commented Dec 15, 2017 at 11:06
  • Could you provide your render function? – Mikhail Katrin Commented Dec 15, 2017 at 11:08
Add a ment  | 

4 Answers 4

Reset to default 7

Easiest would be to have the default be undefined rather than an empty array. Then in your render check:

  • undefined means its loading
  • empty array means no data, so show message
  • non-empty array you can shown data

{!this.state.offer ? <Loader /> : this.state.offer.length ? <OfferList /> : <span>No data available</span>}

Best approach would be to use the ternary operation. First initialize offer to be undefined and you API returns an empty array if there is no data. Next use below expression in render method().

{this.state.offer === undefined ? <Loader /> : this.state.offer.length > 0 ? <OfferList /> : "No data Available"}

Ideally, you should not show or hide the loader on the basis of the size of your array it should depend on your AJAX call.

Simply you can put a variable in your state like "isLoading", mark it true on making ajax call and mark it false on response independent of whether it is a success or failure.

this.state ={ offer: [], isLoading: false }

on ajax, call

this.setState({isLoading:true});

on the response, call

this.setState({isLoading:true});

and you can display

no data available on the basis of size of array.

Add another property in your state denoting processing like below

state = {
  offer: [],
  processing: false
}

then use this property to show/hide your loader.

You can set/unset this property, while you make API call.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>