如何将基于 class 的单选按钮组更改为基于功能的单选按钮组

how can i change class based radio button group to functional based radio group

这是基于 class 的广播组。我想将其转换为基于功能的无线电组。我该怎么做。代码如下:

import React, { Component } from "react";

class Demo2 extends Component {
  constructor() {
    super();
    this.state = {
      name: "React"
    };
    this.onValueChange = this.onValueChange.bind(this);
    this.formSubmit = this.formSubmit.bind(this);
  }

  onValueChange(event) {
    this.setState({
      selectedOption: event.target.value
    });
  }

  formSubmit(event) {
    event.preventDefault();
    console.log(this.state.selectedOption)
  }

  render() {
    return (
      <form onSubmit={this.formSubmit}>
        <div className="radio">
          <label>
            <input
              type="radio"
              value="Male"
              checked={this.state.selectedOption === "Male"}
              onChange={this.onValueChange}
            />
            Male
          </label>
        </div>
        <div className="radio">
          <label>
            <input
              type="radio"
              value="Female"
              checked={this.state.selectedOption === "Female"}
              onChange={this.onValueChange}
            />
            Female
          </label>
        </div>

我想要它基于功能的组件的工作代码。我是新来的,请帮忙。

使用 React.useState 创建本地状态并根据更新后的状态更改模板。

*注意:- 函数组件不需要 this 运算符。 您也可以直接 return 模板(不需要渲染方法)

示例代码:

import React, { useState } from "react";
import "./styles.css";

export default function Demo2() {
  const [selectedOption, setSelectedOption] = useState("Male");

  const onValueChange = (event) => {
    setSelectedOption(event.target.value);
  };

  const formSubmit = (event) => {
    event.preventDefault();
    console.log(selectedOption);
  };

  return (
    <form onSubmit={formSubmit}>
      <div className="radio">
        <label>
          <input
            type="radio"
            value="Male"
            checked={selectedOption === "Male"}
            onChange={onValueChange}
          />
          Male
        </label>
      </div>
      <div className="radio">
        <label>
          <input
            type="radio"
            value="Female"
            checked={selectedOption === "Female"}
            onChange={onValueChange}
          />
          Female
        </label>
      </div>
      <div>
        <button type="submit">Submit</button>
      </div>
    </form>
  );
}

工作代码 - https://codesandbox.io/s/bold-platform-yr3l9?file=/src/App.js:0-1001

import React, { useState } from "react";

function Demo2() {
  const [checked, setChecked] = useState("Male");

  const onValueChange = (event) => {
    setChecked(event.target.value);
  };

  const formSubmit = (event) => {
    event.preventDefault();
    console.log(checked);
  };

  return (
    <form onSubmit={formSubmit}>
      <div className="radio">
        <label>
          <input
            type="radio"
            value="Male"
            checked={checked === "Male"}
            onChange={onValueChange}
          />
          Male
        </label>
      </div>
      <div className="radio">
        <label>
          <input
            type="radio"
            value="Female"
            checked={checked === "Female"}
            onChange={onValueChange}
          />
          Female
        </label>
      </div>
      <div>
        <button type="submit">Submit</button>
      </div>
    </form>
  );
}