Instead of merging changes I make to a state object, React is overwriting it completely.
Could this be caused by the context? I'm not sure if I can pass a dispatch event directly as a prop. I've tried wrapping setParams
in another function, like in the "lifting state up" docs, however to no effect.
Expected Output
{width: 400, height: 400, strokeWeight: 0.25, color: "#d9eb37", resolution: 10}
Current Output
{color: "#d9eb37"}
React context holds the state
const Context = createContext();
const Provider = ({ children }) => {
const [params, setParams] = useState({
width: 400,
height: 400,
strokeWeight: 0.25,
color: "#ad3e00",
resolution: 10
});
return (
<Context.Provider value={{ params, setParams }}>
{children}
</Context.Provider>
);
};
Generic input component
const Input = () => {
const { params, setParams } = useContext(Context);
render(
<input
type="color"
value={params.color}
onChange={(e) => setParams({ color: e.target.value })}
/>
);
}