Reactjs+蚂蚁设计:连接两个ANTD组件
我是一个新手,我需要一个问题,我需要从Ant Design中的Ant Design中连接两个组件(滑块和无线电按钮),并将它们合作在一起。因此,如果我选择“单选”按钮的选项之一,我希望滑块可以调整选择并相反。例如,如果我在第2022号广播按钮上选择,我希望滑块向右移动,例如,如果我在滑块上选择2022,我想检查单个单选按钮值2022,依此类推...试图将它们置于某些条件下,但真的不知道该怎么做。并具有与滑块相同宽度的无线电按钮。
import React from "react";
import { Radio, Slider } from "antd";
const App = () => {
const [currentValueRadio, setcurrentValueRadio] = React.useState(1);
const [currentValue, setCurrentValue] = React.useState();
return (
<>
<Radio.Group
onChange={(e) => {
console.log("radio checked", e.target.value);
setcurrentValueRadio(e.target.value);
}}
value={currentValueRadio}
>
<Radio value={1}>2021</Radio>
<Radio value={2}>2022</Radio>
</Radio.Group>
<Slider
defaultValue={2021}
min={2021}
max={2022}
style={{ width: 240 }}
onChange={(value) => {
console.log(currentValue);
setCurrentValue(value);
}}
/>
</>
);
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将被控制为“值”支柱的状态添加到两个组件中,以使它们合并。
在您的示例中添加值支柱将使两个组件都对相同的状态值做出反应。
但是,由于无线电组件将值设置为1或2,并且滑块上的范围是2021年至2022年,因此您可能看不到所需的结果。也许将无线电值分别更改为2021和2022。
You need to add the state being controlled as a 'value' prop to both components, to have them syncronised.
Adding the value prop in your example will have both components react to the same state value.
However, since the Radio components set the value to 1 or 2, and the range on the Slider is from 2021 to 2022, you may not see the result you're looking for. Perhaps change the Radio values to 2021 and 2022 respectively.