如何将React类组件中的方法转换为函数

发布于 2025-02-11 03:13:09 字数 2143 浏览 1 评论 0原文

我正在使用React来构建雷达图。发现此可编辑 source 。但这是在React类中。我需要将其转换为功能组件。

原始类组件:在此处输入链接说明

我尝试转换课程组件到功能组件,但找不到一种转换类中以下两种方法的方法。

  constructor(props) {
    super(props);
    this.state = {
      data: this.processData(characterData),
      maxima: this.getMaxima(characterData)
    };
  }

  getMaxima(data) {
    const groupedData = Object.keys(data[0]).reduce((memo, key) => {
      memo[key] = data.map((d) => d[key]);
      return memo;
    }, {});
    return Object.keys(groupedData).reduce((memo, key) => {
      memo[key] = Math.max(...groupedData[key]);
      return memo;
    }, {});
  }

  processData(data) {
    const maxByGroup = this.getMaxima(data);
    const makeDataArray = (d) => {
      return Object.keys(d).map((key) => {
        return { x: key, y: d[key] / maxByGroup[key] };
      });
    };
    return data.map((datum) => makeDataArray(datum));
  }

这就是我到目前为止尝试的:

    const [data, setData] = useState(characterData);
const [maxima, setMaxima] = useState(characterData);

  setMaxima = (data: any[]) => {
    const groupedData = Object.keys(data[0]).reduce((memo, key) => {
      memo[key] = data.map(d => d[key]);
      return memo;
    }, {});
    return Object.keys(groupedData).reduce((memo, key) => {
      memo[key] = Math.max(...groupedData[key]);
      return memo;
    }, {});
  };

  setData = (data: any[]) => {
    const maxByGroup = setMaxima(data);
    const makeDataArray = (d: { [x: string]: number }) =>
      Object.keys(d).map(key => ({ x: key, y: d[key] / maxByGroup[key] }));
    return data.map(datum => makeDataArray(datum));
  };

有人可以帮助我将这两种方法转换为功能。这里的问题是这些是USESTATE函数的设定方法。

I'm using react to build a radar chart. found this editable source. But it is in react class components. I need to convert it to functional components.

Original Class component: enter link description here

I tried converting the class component to functional component but could not find a way to convert the following two methods in the class.

  constructor(props) {
    super(props);
    this.state = {
      data: this.processData(characterData),
      maxima: this.getMaxima(characterData)
    };
  }

  getMaxima(data) {
    const groupedData = Object.keys(data[0]).reduce((memo, key) => {
      memo[key] = data.map((d) => d[key]);
      return memo;
    }, {});
    return Object.keys(groupedData).reduce((memo, key) => {
      memo[key] = Math.max(...groupedData[key]);
      return memo;
    }, {});
  }

  processData(data) {
    const maxByGroup = this.getMaxima(data);
    const makeDataArray = (d) => {
      return Object.keys(d).map((key) => {
        return { x: key, y: d[key] / maxByGroup[key] };
      });
    };
    return data.map((datum) => makeDataArray(datum));
  }

This is what I've tried so far:

    const [data, setData] = useState(characterData);
const [maxima, setMaxima] = useState(characterData);

  setMaxima = (data: any[]) => {
    const groupedData = Object.keys(data[0]).reduce((memo, key) => {
      memo[key] = data.map(d => d[key]);
      return memo;
    }, {});
    return Object.keys(groupedData).reduce((memo, key) => {
      memo[key] = Math.max(...groupedData[key]);
      return memo;
    }, {});
  };

  setData = (data: any[]) => {
    const maxByGroup = setMaxima(data);
    const makeDataArray = (d: { [x: string]: number }) =>
      Object.keys(d).map(key => ({ x: key, y: d[key] / maxByGroup[key] }));
    return data.map(datum => makeDataArray(datum));
  };

Can somebody please help me to convert these two methods to functions. The problem here is these are the set methods of useState functions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天涯离梦残月幽梦 2025-02-18 03:13:10

您必须在功能组件中的const中提及功能。尝试下面的尝试,

  const [data, setData] = useState(characterData);
  const [maxima, setMaxima] = useState(characterData);

  const setMaxima1 = (data: any[]) => {
    const groupedData = Object.keys(data[0]).reduce((memo, key) => {
      memo[key] = data.map(d => d[key]);
      return memo;
    }, {});
    setMaxima(value)
    return Object.keys(groupedData).reduce((memo, key) => {
      memo[key] = Math.max(...groupedData[key]);
      return memo;
    }, {});

  };

  const setData1 = (data: any[]) => {
    const maxByGroup = setMaxima(data);
    const makeDataArray = (d: { [x: string]: number }) =>
      Object.keys(d).map(key => ({ x: key, y: d[key] / maxByGroup[key] }));
    setData(Value)
    return data.map(datum => makeDataArray(datum));
  };

You have to mention the functions in const in functional components. Try like below,

  const [data, setData] = useState(characterData);
  const [maxima, setMaxima] = useState(characterData);

  const setMaxima1 = (data: any[]) => {
    const groupedData = Object.keys(data[0]).reduce((memo, key) => {
      memo[key] = data.map(d => d[key]);
      return memo;
    }, {});
    setMaxima(value)
    return Object.keys(groupedData).reduce((memo, key) => {
      memo[key] = Math.max(...groupedData[key]);
      return memo;
    }, {});

  };

  const setData1 = (data: any[]) => {
    const maxByGroup = setMaxima(data);
    const makeDataArray = (d: { [x: string]: number }) =>
      Object.keys(d).map(key => ({ x: key, y: d[key] / maxByGroup[key] }));
    setData(Value)
    return data.map(datum => makeDataArray(datum));
  };
留蓝 2025-02-18 03:13:10

最明显的事情是将它们写成常规功能,然后在代码中删除所有,因为它们已经是相当纯净的函数:

  function MyComponent (props) {
    [data, setData] = useState(processData(characterData));
    [maxima, setMaxima] = useState(getMaxima(characterData));

    function getMaxima (data) {
      const groupedData = Object.keys(data[0]).reduce((memo, key) => {
        memo[key] = data.map((d) => d[key]);
        return memo;
      }, {});
      return Object.keys(groupedData).reduce((memo, key) => {
        memo[key] = Math.max(...groupedData[key]);
        return memo;
      }, {});
    }

    function processData (data) {
      const maxByGroup = getMaxima(data);
      const makeDataArray = (d) => {
        return Object.keys(d).map((key) => {
          return { x: key, y: d[key] / maxByGroup[key] };
        });
      };
      return data.map((datum) => makeDataArray(datum));
    }
  }

The most obvious thing to do is to just write them as regular functions and remove all the this in the code because they are already fairly pure functions:

  function MyComponent (props) {
    [data, setData] = useState(processData(characterData));
    [maxima, setMaxima] = useState(getMaxima(characterData));

    function getMaxima (data) {
      const groupedData = Object.keys(data[0]).reduce((memo, key) => {
        memo[key] = data.map((d) => d[key]);
        return memo;
      }, {});
      return Object.keys(groupedData).reduce((memo, key) => {
        memo[key] = Math.max(...groupedData[key]);
        return memo;
      }, {});
    }

    function processData (data) {
      const maxByGroup = getMaxima(data);
      const makeDataArray = (d) => {
        return Object.keys(d).map((key) => {
          return { x: key, y: d[key] / maxByGroup[key] };
        });
      };
      return data.map((datum) => makeDataArray(datum));
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文