在观看结束时做出反应,javascript,reactjs,typescript,redux,react-hooks"/>

首页 > 解决方案 > 我的价值越来越在观看结束时做出反应

问题描述

当代码完成时,cData 的值变空,当我在它之间进行调试时,它会获取数据

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);
  });
});
.......

我也在 el.Consumer 中获取数据,当我在控制台中观看它时,它获取了数据,但最后它显示我在监视 cData

标签: javascriptreactjstypescriptreduxreact-hooks

解决方案


在动作文件中试试这个而不是在这里做

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

而不是 tempData 使用带有您想要的索引的响应。


推荐阅读