michael
michael

Reputation: 56

My value is getting <not available> at the end of watch in react

the value of cData is getting empty when the code finished , when i debug in between it gets the data

const FsAccount = React.memo(() => {
  let cData: any;
  const accountData = useSelector((state: any) => {
    let tempData = state.account.customerDataByAccount;
    // tempData = JSON.stringify(tempData)
    let final: any = [];
    tempData.forEach((el: any) => {
      Object.keys(el).forEach((key) => {
        let values: any = [];

        Object.entries(el[key][0]).forEach((entry) => {
          values.push({ title: entry[0], value: entry[1] });
        });
        let newElement: any = {};
        newElement[key] = values;
        final.push(newElement);
        final.map((el: any) => {
          if (el.Consumer) {
            cData = el.Consumer; // here it should have el.Consumer but after execution its getting empty
          }
        });
      });
    });

    console.log("state :>> ", cData);
  });
});
.......

also i am getting data in el.Consumer and when i was watching it in console it getting the data but at the end its showing me in watch for cData

Upvotes: 1

Views: 55

Answers (1)

Mohammad Zubair
Mohammad Zubair

Reputation: 413

try this in action file instead of doing it here

   let tempData = state.account.customerDataByAccount;
    // tempData = JSON.stringify(tempData)
    let final: any = [];
    tempData.forEach((el: any) => {
      Object.keys(el).forEach((key) => {
        let values: any = [];

        Object.entries(el[key][0]).forEach((entry) => {
          values.push({ title: entry[0], value: entry[1] });
        });
        let newElement: any = {};
        newElement[key] = values;
        final.push(newElement);
        final.map((el: any) => {
          if (el.Consumer) {
            cData = el.Consumer; // here it should have el.Consumer but after execution its getting empty
          }
        });
      });
    });

instead of tempData use the response with the index you want.

Upvotes: 1

Related Questions