【发布时间】:2021-01-26 05:30:47
【问题描述】:
这是我的反应组件“类”基本投票的代码,但我想将此表单转换为反应挂钩,所以需要帮助......!我不明白我该怎么做?
import React, { Component } from "react";
import Poll from "react-polls";
// Declaring poll question and answers
const pollQuestion = "Youtube is the best place to learn ?";
const pollAnswers = [
{ option: "Yes", votes: 7 },
{ option: "No", votes: 2 },
{ option: "don't know", votes: 1 },
];
class Fakepolls extends Component {
// Setting answers to state to reload the component with each vote
state = {
pollAnswers: [...pollAnswers],
};
// Handling user vote
// Increments the votes count of answer when the user votes
handleVote = (voteAnswer) => {
const { pollAnswers } = this.state;
const newPollAnswers = pollAnswers.map((answer) => {
if (answer.option === voteAnswer) answer.votes++;
return answer;
});
this.setState({
pollAnswers: newPollAnswers,
});
};
render() {
const { pollAnswers } = this.state;
return (
<div>
<Poll
question={pollQuestion}
answers={pollAnswers}
onVote={this.handleVote}
/>
</div>
);
}
}
export default Fakepolls;
【问题讨论】:
-
有什么可以转换成 React 钩子?您的意思是将基于类的组件转换为功能组件吗?您尝试过哪些不起作用的方法?
-
是的,你是对的..!基于类的组件到功能组件
标签: node.js reactjs react-native express mern