当前位置:首页 > React

react 单选实现

2026-01-26 14:22:12React

实现 React 单选功能

在 React 中实现单选功能可以通过多种方式完成,以下是常见的几种方法:

使用受控组件(Controlled Component)

通过状态管理单选按钮的选中状态,适合表单场景:

import { useState } from 'react';

function RadioGroup() {
  const [selectedOption, setSelectedOption] = useState('option1');

  const handleChange = (e) => {
    setSelectedOption(e.target.value);
  };

  return (
    <div>
      <label>
        <input
          type="radio"
          value="option1"
          checked={selectedOption === 'option1'}
          onChange={handleChange}
        />
        Option 1
      </label>
      <label>
        <input
          type="radio"
          value="option2"
          checked={selectedOption === 'option2'}
          onChange={handleChange}
        />
        Option 2
      </label>
    </div>
  );
}

使用非受控组件(Uncontrolled Component)

利用 HTML 原生 radio 的互斥特性,适合简单场景:

react 单选实现

function RadioGroup() {
  return (
    <div>
      <label>
        <input type="radio" name="options" value="option1" defaultChecked />
        Option 1
      </label>
      <label>
        <input type="radio" name="options" value="option2" />
        Option 2
      </label>
    </div>
  );
}

使用第三方库

对于复杂场景,可以使用如 react-radio-group 等专门库:

import { RadioGroup, Radio } from 'react-radio-group';

function MyRadioGroup() {
  const [value, setValue] = useState('apple');

  return (
    <RadioGroup name="fruits" selectedValue={value} onChange={setValue}>
      <Radio value="apple" /> Apple
      <Radio value="orange" /> Orange
    </RadioGroup>
  );
}

自定义 Radio 组件

react 单选实现

创建可复用的自定义 Radio 组件:

function CustomRadio({ value, checked, onChange, children }) {
  return (
    <label>
      <input
        type="radio"
        value={value}
        checked={checked}
        onChange={onChange}
      />
      {children}
    </label>
  );
}

function RadioGroup() {
  const [selected, setSelected] = useState('A');

  return (
    <div>
      <CustomRadio 
        value="A" 
        checked={selected === 'A'}
        onChange={() => setSelected('A')}
      >
        Option A
      </CustomRadio>
      <CustomRadio 
        value="B" 
        checked={selected === 'B'}
        onChange={() => setSelected('B')}
      >
        Option B
      </CustomRadio>
    </div>
  );
}

样式优化技巧

为单选按钮添加自定义样式时,常用的 CSS 方案:

/* 隐藏原生 radio 用伪元素自定义样式 */
input[type="radio"] {
  opacity: 0;
  position: absolute;
}

input[type="radio"] + label::before {
  content: "";
  display: inline-block;
  width: 16px;
  height: 16px;
  margin-right: 8px;
  border: 2px solid #ccc;
  border-radius: 50%;
  vertical-align: middle;
}

input[type="radio"]:checked + label::before {
  background-color: #007bff;
  border-color: #007bff;
}

无障碍访问建议

确保单选组符合 WAI-ARIA 规范:

<fieldset>
  <legend>选择性别</legend>
  <div role="radiogroup">
    <input id="male" type="radio" name="gender" value="male" />
    <label htmlFor="male">男性</label>
    <input id="female" type="radio" name="gender" value="female" />
    <label htmlFor="female">女性</label>
  </div>
</fieldset>

标签: 单选react
分享给朋友:

相关文章

vue如何实现单选

vue如何实现单选

Vue 实现单选的方法 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-model 绑定单选按钮 通过 v-model 绑定到同一个变量,确保同一时间只有一个选…

vue单选按钮实现

vue单选按钮实现

Vue 单选按钮实现方法 在 Vue 中实现单选按钮可以通过 v-model 指令绑定数据,结合 <input type="radio"> 或第三方组件库完成。以下是几种常见实现方式:…

react native如何启动

react native如何启动

如何启动 React Native 项目 安装 Node.js 和 npm 确保已安装 Node.js(建议版本 16 或更高)和 npm(Node.js 自带)。可通过以下命令检查版本: node…

如何评价react native

如何评价react native

React Native 的优势 跨平台开发效率高:基于 JavaScript 和 React 语法,可同时开发 iOS 和 Android 应用,减少重复代码量。性能接近原生:通过原生组件渲染,性能…

react中monent如何获取日期

react中monent如何获取日期

使用 Moment.js 获取当前日期 在 React 中通过 Moment.js 获取当前日期,可以直接调用 moment() 函数。它会返回包含当前日期和时间的 Moment 对象。 impor…

react 如何执行

react 如何执行

安装 Node.js 和 npm React 开发需要 Node.js 环境,因为它提供了 npm(或 yarn)包管理工具。从 Node.js 官网 下载并安装最新 LTS 版本。安装完成后,在终端…