2类型:单击react时会更改颜色的无线电按钮
我想制作一个组件,有两个选项,当您选择一个选项时,按钮的颜色被切换了,目前,我正在尝试使用类型的输入:无线电,但是我想知道是否有任何另一种与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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此示例采用性别状态(默认为女孩),并设置一个const,该const检查性别是否等于女孩,它将返回粉红色组件,而其他将返回蓝色组件。 ONCLICK函数将状态更改为性别而不是。 (女孩或男孩)
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)