I have a section in which I want users to be able to change the background color using react color picker and react-redux, unfortunately, the background color is not changing / not updating to a new color.
Live code in codesandbox: change background color
Source code:
import React, { useState, useEffect } from "react";
import { useSelector, useDispatch } from "react-redux";
import { ChromePicker } from "react-color";
import Details from "./Details";
const Settings = () => {
const fields = useSelector((state) => state);
const dispatch = useDispatch();
const [colorRgb, setColorRgb] = useState("#000");
useEffect(() => {
updateColor();
}, []);
const updateColor = () => {
dispatch({
type: "change",
name: "socialColor",
value: colorRgb
});
};
console.log("current color", colorRgb);
console.log("current color", fields.color);
return (
<div>
<div>
<Details />
<h1>Edit </h1>
<div className="container">
<ChromePicker
renderers={false}
color={colorRgb}
onChange={(e) => setColorRgb(e.hex)}
/>
<span
className="testa"
style={{
background: fields.color,
color: "#fff"
}}
>
Change my background color
</span>
</div>
</div>
</div>
);
};
export default Settings;
What is wrong with my code.?
The problem will be fixed if you get the hex code instead of rgb
your code
must be