1

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 />
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 Commented Dec 15, 2017 at 11:06
  • Could you provide your render function? Commented Dec 15, 2017 at 11:08

4 Answers 4

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>}

Sign up to request clarification or add additional context in comments.

4 Comments

That's how to do it. Thanks for this smart solution!
yes you are right but is it possible to set default state undefined in Redux?
this is react state, if this was redux state it would be passed in via props
@NarayananS If you were to do this via redux and props its still doable even with redux's restrictions, except instead of undefined as your reducer's default, you would use null, as this is an acceptable return value from reducer. The logic being it being that undefined is disallowed because it could just be no state passed at all to the reducer arg, while null is an explicit value that has been set. I use this pattern for exactly this purpose and works a treat
2

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"}

1 Comment

indeed this is the best approach this answer worth more than the accepted answer
0

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.

Comments

-1

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.

10 Comments

Flags are harmful. If there's any way to compute boolean values this is the right way to go. So have a look @alechill s answer which is quite good.
Would you try to explain, why flags are harmful?
In simple words you have now 1 more state variable to handle just for hide/ show.
That is ok and I guess it is personal preference. But I didn't get why it is harmful.
Yes of course - sorry for this statement without any explanation :-D I think you are aware of the single responsibility principle. It says that each function should have exactly one functionality and not two or more. Any boolean value that you pass into a class or function can be splitted into two functions or two classes which then have a single resposibility. So each time you use a boolean value check if it's really necessary or if you oversee a huge problem in your whole app architecture which is often the case
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.