如何将React类组件中的方法转换为函数
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在功能组件中的
const
中提及功能。尝试下面的尝试,You have to mention the functions in
const
in functional components. Try like below,最明显的事情是将它们写成常规功能,然后在代码中删除所有
此
,因为它们已经是相当纯净的函数: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: