react antd 怎样用颜色选择器关联背景颜色
时间: 2023-07-04 09:07:08 浏览: 228
可以使用onChange
事件来监听颜色选择器的变化,然后更新组件的style
属性中的backgroundColor
值。具体实现可以参考以下代码:
import React, { useState } from 'react';
import { SketchPicker } from 'react-color';
import { Button } from 'antd';
function ColorPicker() {
const [color, setColor] = useState('#ffffff');
const handleChange = (newColor) => {
setColor(newColor.hex);
};
const handleButtonClick = () => {
// do something with the selected color
};
return (
<div style={{ backgroundColor: color }}>
<SketchPicker color={color} onChange={handleChange} />
<Button onClick={handleButtonClick}>Save</Button>
</div>
);
}
上述代码中,SketchPicker
组件是react-color
库中提供的颜色选择器组件,它的onChange
事件会返回一个包含选中颜色信息的对象,我们通过setColor
方法将选中颜色的hex
值保存到color
状态中。然后将该值作为style
属性中的backgroundColor
值,实现了背景颜色的关联。最后,点击保存按钮时可以将选中的颜色值发送到后端进行处理。
相关推荐

















