2类型:单击react时会更改颜色的无线电按钮

发布于 2025-01-27 09:16:17 字数 1021 浏览 0 评论 0原文

我想制作一个组件,有两个选项,当您选择一个选项时,按钮的颜色被切换了,目前,我正在尝试使用类型的输入:无线​​电,但是我想知道是否有任何另一种与React一起做的方法。

import React, {useState} from 'react'

function BotonCambioPerfil() {

    const [gender, setGender] = React.useState('Famale');

    const handleCatChange = () => {
        setGender('Male');
      };
    
      const handleDogChange = () => {
        setGender('Female');
      };

  return (
    <div className='contenedorBotonCambioPerfil'>

    <RadioButton
        label="Male"
        value={gender === 'Male'}
        onChange={handleCatChange}
      />
      <RadioButton
        label="Female"
        value={gender === 'Female'}
        onChange={handleDogChange}
      />
        
        </div>
  )
}

const RadioButton = ({ label, value, onChange }) => {
    return (
      <label>
        <input type="radio" checked={value} onChange={onChange} />
        {label}
      </label>
    );
  };
  
  
export default BotonCambioPerfil

I want to make a component, where there are two options, and when you select one, the color of the buttons gets switched, currently, I'm trying with Inputs of type: radio, but I would like to know if there is any other way to do it with react.

import React, {useState} from 'react'

function BotonCambioPerfil() {

    const [gender, setGender] = React.useState('Famale');

    const handleCatChange = () => {
        setGender('Male');
      };
    
      const handleDogChange = () => {
        setGender('Female');
      };

  return (
    <div className='contenedorBotonCambioPerfil'>

    <RadioButton
        label="Male"
        value={gender === 'Male'}
        onChange={handleCatChange}
      />
      <RadioButton
        label="Female"
        value={gender === 'Female'}
        onChange={handleDogChange}
      />
        
        </div>
  )
}

const RadioButton = ({ label, value, onChange }) => {
    return (
      <label>
        <input type="radio" checked={value} onChange={onChange} />
        {label}
      </label>
    );
  };
  
  
export default BotonCambioPerfil

How I want the component to look

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-02-03 09:16:17

此示例采用性别状态(默认为女孩),并设置一个const,该const检查性别是否等于女孩,它将返回粉红色组件,而其他将返回蓝色组件。 ONCLICK函数将状态更改为性别而不是。 (女孩或男孩)

import React, { useState } from 'react'

export default function App() {

  const [ gender , setGender ] = useState("girl")

  const changeGender = () => {
    if(gender === "girl") {
      setGender("boy")
    } else setGender("girl")
  }

  const genderBtn = gender === "girl" ?
  <h2 onClick={changeGender} style={{
    backgroundColor: "pink",
    width: "100px",
    padding: "2px",
    borderRadius: "100px",
    cursor: "pointer",
  }}>{gender}</h2>
  :
  <h2 onClick={changeGender} style={{
    backgroundColor: "lightblue",
    width: "100px",
    padding: "2px",
    borderRadius: "100px",
    cursor: "pointer",
  }}>{gender}</h2>

  return (
    <div className="App">
      <h1>Gender Switch</h1>
      {genderBtn}
    </div>
  );
}

this example takes a state of gender (default to girl), and sets a const that checks if gender is equal to girl it will return the pink component else it will return a blue component. The onclick function changes the state to the gender its not. (girl or boy)

import React, { useState } from 'react'

export default function App() {

  const [ gender , setGender ] = useState("girl")

  const changeGender = () => {
    if(gender === "girl") {
      setGender("boy")
    } else setGender("girl")
  }

  const genderBtn = gender === "girl" ?
  <h2 onClick={changeGender} style={{
    backgroundColor: "pink",
    width: "100px",
    padding: "2px",
    borderRadius: "100px",
    cursor: "pointer",
  }}>{gender}</h2>
  :
  <h2 onClick={changeGender} style={{
    backgroundColor: "lightblue",
    width: "100px",
    padding: "2px",
    borderRadius: "100px",
    cursor: "pointer",
  }}>{gender}</h2>

  return (
    <div className="App">
      <h1>Gender Switch</h1>
      {genderBtn}
    </div>
  );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文