I want to change the color of the ‘left–innerTopBox’ whenever the changes.
I believe im missing a handleChange function and an onChange. THANKS for you help.
import "./App.css";
import { useState } from "react";
function App() {
const [color, setColor] = useState({
color1: "#fff",
});
return (
<>
<div className="box--control">
<div className="big--leftBox">
left box
<div className="left--innerTopBox" value={color.color1}>
color here
</div>
<div className="left--innerBottomBox">code here</div>
</div>
<div className="big--rightBox">
right box
<div className="inputs">
Color 1
<input
type="color"
style={{ marginLeft: 10 }}
value={color.color1}
/>
</div>
<div className="inputs">
Color 2<input type="color" style={{ marginLeft: 10 }} />
</div>
</div>
</div>
</>
);
}
export default App;
2
Answers
Try something like this:
A
<div>
has novalue
:Presumably you meant to use a
style
?:Then all you need to do is update the state value to whatever you want. For example:
This can be abstracted into its own function, for example:
and:
Note also that this over-writes the entirety of the state. If you end up with multiple properties in that state object, you can preserve them and update only one:
Of course this also hard-codes the property name. You can make that dynamic by giving the input a
name
:And using that
name
in the function:Which would allow you to re-use the same change handler for multiple inputs that set different properties on the same state object.
Note of course that this will update it on every change. So for example if you type "green" then there will 5 "color" updates:
{ color1: 'g' }
{ color1: 'gr' }
{ color1: 'gre' }
{ color1: 'gree' }
{ color1: 'green' }