【问题标题】:react js class poll convert into react hooks pollreact js 类 poll 转换成 react hooks poll
【发布时间】: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


【解决方案1】:

对于这个答案,我假设您想问的是如何将基于类的组件转换为函数式组件,因为没有任何东西可以真正转换为自定义反应钩子。

转换步骤:

  1. 转换状态以使用useState React 钩子。
  2. this.state.pollAnswers 的引用替换为pollAnswers
  3. 将对this.setState 的引用替换为setPollAnswers
  4. 使用适当的功能状态更新改变现有状态。
  5. this.handleVote 的引用替换为handleVote 并声明const

代码

import React, { useState } from "react";
import Poll from "react-polls";

// Declaring poll question and answers
const pollQuestion = "Youtube is the best place to learn ?";
const answers = [ // <-- renamed to avoid collision with state variable
  { option: "Yes", votes: 7 },
  { option: "No", votes: 2 },
  { option: "don't know", votes: 1 }
];

const Fakepolls = () => {
  // Setting answers to state to reload the component with each vote
  const [pollAnswers, setPollAnswers] = useState([...answers]);

  // Handling user vote
  // Increments the votes count of answer when the user votes
  const handleVote = (voteAnswer) => {
    setPollAnswers((pollAnswers) =>
      pollAnswers.map((answer) =>
        answer.option === voteAnswer
          ? {
              ...answer,
              votes: answer.votes + 1
            }
          : answer
      )
    );
  };

  return (
    <div>
      <Poll
        question={pollQuestion}
        answers={pollAnswers}
        onVote={handleVote}
      />
    </div>
  );
};
export default Fakepolls;

【讨论】:

  • ReferenceError: 在初始化之前无法访问'pollAnswers' const Fakepolls = () => { 13 | // 设置状态答案以在每次投票 > 14 时重新加载组件 | const [pollAnswers, setPollAnswers] = useState([...pollAnswers]); 15 | 16 | // 处理用户投票 17 | // 当用户投票时增加 answer 的投票数
  • @Asitsingh 啊,是的,我也击中了那个...在沙箱中,我刚刚将const pollAnswers 重命名为const answers,以避免与状态变量pollAnswers 发生名称冲突。很抱歉没有在沙箱中明确指出这一点。
猜你喜欢
  • 2017-08-28
  • 2019-03-05
  • 1970-01-01
  • 2021-12-29
  • 1970-01-01
  • 1970-01-01
  • 2011-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多