Data from API call are stored in a Array but when i try to use that array in a function for further use it shows that array is empty . why?

Ram Aravind

React code for storing Data from API to an Array and Using the same Array's event_date value for further use.

export const UpcomingHolidays = (props: UpcomingHolidaysProps) => {
  const [holidayPlans, setHolidayPlans] = useState([]);

  const [dateArray, setDate] = useState([]);

  useEffect(() => {
    getHolidayPlans();
  }, []);

  const getHolidayPlans = async () => {
    const holidayResp = await PortalHolidayService.getInstance().getHolidayPlans();
    if (holidayResp) {
      setCities(() => holidayResp.cityModule);
      setHolidayPlans(() => holidayResp.holidayModule);
      setDate(() => holidayResp.holidayModule);
    }
    let today = new Date();
    console.log(holidayPlans);
    holidayPlans.filter((date) => {
      const eventDate = new Date(date.event_date);
      console.log(eventDate);
    });
  };

So what the thing is when i use the Same (holidayPlans) array to display some contents in html it shows the values and displays properly but when i use inside a function it shows there is no data inside the array .

console.log(holidayPlans) shows this

Same Array used to display in html

Jacob

Here's a challenge: write a JavaScript function useState such that the console.log outputs a 4 and then a 5:

function render() {
  let [thing, setThing] = useState(4);
  console.log(thing); // 4
  setThing(5);
  console.log(thing); // 5
}

No matter what you do, you'll never be able to write this function, because no external JavaScript function will be able to set the value of the thing variable; that's because an external JavaScript has no way to modify the thing variable. All useState would be able to do is set its own internal state and change what it returns. Silly example here:

let hiddenState;
function useState(initialValue) {
  if (hiddenState === undefined) {
    hiddenState = initialValue;
  }
  const setState = value => {
    hiddenState = value;
  }
  return [hiddenState, setState];
}

That means render will only be able to get a new value if useState is called again:

function render() {
  let [thing, setThing] = useState(4);
  console.log(thing); // 4
  setThing(5);

  [thing, setThing] = useState(4);
  console.log(thing); // 5
}

This is essentially what useState does but in a way where the hidden state is unique per instance. As you can see, setState is to be considered "asynchronous" in that state changes aren't reflected until the next render. setState queues up a re-render request. The next time your render function is called, useState will be called again, and it will return a new value.

Notice with these code modifications, rather than us referencing the state variable before it has updated, we can still reference your response object to get the data:

export const UpcomingHolidays = (props: UpcomingHolidaysProps) => {

  // On the first rendering of `UpcomingHolidays`, holidayPlans will be [].
  // After setHolidayPlans is called, a re-render will be queued, and this
  // UpcomingHolidays  function will be called again. When useState is called
  // the second time, it will have the value passed into setHolidayPlans.
  const [holidayPlans, setHolidayPlans] = useState([]);

  // Same for dateArray.
  const [dateArray, setDate] = useState([]);

  useEffect(() => {
    getHolidayPlans();
  }, []);

  async function getHolidayPlans() {
    const holidayResp = await PortalHolidayService.getInstance().getHolidayPlans();
    if (!holidayResp) {
      return;
    }

    // These will flag the component as needing to re-render after the effect
    // completes. They do not change the local variables; they update the
    // internal data of the useState hooks so that the next time those useState
    // calls occur, they'll return new values.
    setCities(holidayResp.cityModule);
    setHolidayPlans(holidayResp.holidayModule);
    setDate(holidayResp.holidayModule.map(date => new Date(date.event_date));

    // If you want to log here, don't reference state, which hasn't updated yet.
    // Either store response data as variables or reference the response itself.
    console.log('Holidays are', holidayResp.holidayModule);
  }

  return <div>Your content</div>;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is this array undefined when I call .length?

From Dev

What data type should I use when converting a byte array from Swift to Objective-C

From Dev

Why do linq search has a huge difference in efficiency when I use string and array especially for large data?

From Dev

Why can I call GetType() on an empty array but not when its returned from a function

From Dev

Why should I use the array memory location when assigning value?

From Dev

Why can't I successfully use 'call' on Array.isArray?

From Dev

Why I can use @list to call an array, but can't use %dict to call a hash in perl?

From Dev

Ruby: How to use array names stored in array

From Dev

Why cant I push inside an array and use it in a function argument in javascript?

From Dev

How to use an if function to call elements of an array

From Dev

why use array($this,'function') instead of $this->function()

From Dev

array return empty when trying to call from outside the function [swift]

From Dev

Use some data from array, if the value is empty, use it from another array

From Dev

Why use " = " for an array method?

From Dev

Why does my code print empty array([]) when I use text files?

From Dev

Can I use _lodash to update an array with data from another?

From Dev

Use array of names from Ajax call in variable

From Dev

Why is both my input array and output array corrupted when I try to implement a function to reverse the array?

From Dev

Why do I get error when I try to alert the length of the passed array to the function?

From Dev

what is happening when try to use array variable

From Dev

wordpress category is empty when i use array_rand variable

From Dev

I could not push an new data into array when use upsert

From Dev

Why I am getting $parse error when use it on object with array?

From Dev

array return empty when trying to call from outside the function [swift]

From Dev

Why I am getting random values from array in a function when returning an array from a function as an argument?

From Dev

what is the data type that i want to use when merging Array when i merge String array and Integer Array into merge Array

From Dev

I keep getting errors when I try to use JavaScript array methods

From Dev

php shows empty array but ajax shows the data

From Dev

Why can’t I call jQuery methods stored within an array?

Related Related

  1. 1

    Why is this array undefined when I call .length?

  2. 2

    What data type should I use when converting a byte array from Swift to Objective-C

  3. 3

    Why do linq search has a huge difference in efficiency when I use string and array especially for large data?

  4. 4

    Why can I call GetType() on an empty array but not when its returned from a function

  5. 5

    Why should I use the array memory location when assigning value?

  6. 6

    Why can't I successfully use 'call' on Array.isArray?

  7. 7

    Why I can use @list to call an array, but can't use %dict to call a hash in perl?

  8. 8

    Ruby: How to use array names stored in array

  9. 9

    Why cant I push inside an array and use it in a function argument in javascript?

  10. 10

    How to use an if function to call elements of an array

  11. 11

    why use array($this,'function') instead of $this->function()

  12. 12

    array return empty when trying to call from outside the function [swift]

  13. 13

    Use some data from array, if the value is empty, use it from another array

  14. 14

    Why use " = " for an array method?

  15. 15

    Why does my code print empty array([]) when I use text files?

  16. 16

    Can I use _lodash to update an array with data from another?

  17. 17

    Use array of names from Ajax call in variable

  18. 18

    Why is both my input array and output array corrupted when I try to implement a function to reverse the array?

  19. 19

    Why do I get error when I try to alert the length of the passed array to the function?

  20. 20

    what is happening when try to use array variable

  21. 21

    wordpress category is empty when i use array_rand variable

  22. 22

    I could not push an new data into array when use upsert

  23. 23

    Why I am getting $parse error when use it on object with array?

  24. 24

    array return empty when trying to call from outside the function [swift]

  25. 25

    Why I am getting random values from array in a function when returning an array from a function as an argument?

  26. 26

    what is the data type that i want to use when merging Array when i merge String array and Integer Array into merge Array

  27. 27

    I keep getting errors when I try to use JavaScript array methods

  28. 28

    php shows empty array but ajax shows the data

  29. 29

    Why can’t I call jQuery methods stored within an array?

HotTag

Archive