I have two input fields. when I press the check box the state value stored in solutestate should be in the first input box. then if I uncheck the box the value in the second input field should be changed. Both the input box should have an independent state change. I am attaching a codesandbox link which I am trying to edit Sandbox link.
import "./styles.css";
import { Jsme } from "jsme-react";
import { useState } from "react";
import { useForm } from "react-hook-form";
import { default as FormData } from "form-data";
export default function App() {
const [input1, setInput1] = useState("");
const [input2, setInput2] = useState("");
const [syncToFirst, setSyncToFirst] = useState();
const [solutestate, setSoluteState] = useState();
const { register, handleSubmit, control } = useForm({
defaultValues: {
solute: "",
solvent: ""
}
});
const formData = new FormData();
return (
<div className="App">
<Jsme
height="300px"
width="400px"
options="oldlook,star"
onChange={setSoluteState}
/>
<h1>{solutestate}</h1>
<input
className="mr-2 leading-tight"
type="checkbox"
onChange={(event) => {
setSyncToFirst(event.target.checked ? 1 : 2);
}}
/>
<input
className="mr-2 leading-tight"
type="checkbox"
onChange={(event) => {
setSyncToFirst(event.target.checked ? 1 : 2);
}}
/>
<form>
<input
{...register("solute")}
placeholder="SOLUTE"
onChange={(e) =>
syncToFirst === 1
? setSoluteState(e.target.value)
: setInput1(e.target.value)
}
value={syncToFirst === 1 ? solutestate : input1}
/>
<input
{...register("solvent")}
placeholder="SOLVENT"
onChange={(e) =>
syncToFirst === 2
? setSoluteState(e.target.value)
: input2(e.target.value)
}
value={syncToFirst === 2 ? solutestate : input2}
/>
</form>
</div>
);
}
I'm not sure I follow you but for starters you should put the checkbox values in state variables. I don't really understand if you care about the sequence of the checks, I.E. which checkbox was checked last.
export default function App() {
// I added the following state variables to keep track of checkboxes
const [isCheckbox1Checked, setIsCheckbox1Checked] = useState(false);
const [isCheckbox2Checked, setIsCheckbox2Checked] = useState(false);
const [input1, setInput1] = useState("");
const [input2, setInput2] = useState("");
const [syncToFirst, setSyncToFirst] = useState();
const [solutestate, setSoluteState] = useState();
const { register, handleSubmit, control } = useForm({
defaultValues: {
solute: "",
solvent: ""
}
});
return (
<div className="App">
<Jsme
height="300px"
width="400px"
options="oldlook,star"
onChange={setSoluteState}
/>
<h1>{solutestate}</h1>
<input
className="mr-2 leading-tight"
type="checkbox"
value={isCheckbox1Checked}
onChange={() => setIsCheckbox1Checked(prev => !prev)}
/>
<input
className="mr-2 leading-tight"
type="checkbox"
value={isCheckbox2Checked}
onChange={() => setIsCheckbox2Checked(prev => !prev)}
/>
<form>
<input
{...register("solute")}
placeholder="SOLUTE"
onChange={(e) =>
syncToFirst === 1
? setSoluteState(e.target.value)
: setInput1(e.target.value)
}
value={isCheckbox1Checked ? solutestate : input1}
/>
<input
{...register("solvent")}
placeholder="SOLVENT"
onChange={(e) =>
syncToFirst === 2
? setSoluteState(e.target.value)
: input2(e.target.value)
}
value={isCheckbox2Checked ? solutestate : input2}
/>
</form>
</div>
);
}
This code still is wierd, could you please explain what behaviour you want from the text input fields?