1

I am trying to build a form with some nested data update. The code is as below:

On Change method

let student = {
        name: null,
        marks: { english: null },
    };
const handleChange = (updatedValue) => {
    student = {
        ...student,
        ...updatedValue,
        marks: { ...updatedValue.marks },
    };
    console.log(student);
};

And my JSX:

<input
                            type="text"
                            className="form-control"
                            id="name"
                            placeholder="Name"
                            value={student.name ?? ""}
                            onChange={(event) =>
                                handleChange({
                                    name: event.target.value,
                                })
                            }
                        />
<input
                                type="text"
                                className="form-control"
                                id="english"
                                placeholder="English"
                                value={student.marks.english ?? ""}
                                onChange={(event) =>
                                    handleChange({
                                        marks: { english: event.target.value },
                                    })
                                }
                            />

The problem is that for "name" it works fine. But for "english", it behaves abnormally. E.g. when I enter "1" in English, it updates it to 1 in the console but on the UI, it immediately disappears from the input box. Also if I update it to 2, the value is replaced from one to 2.

The the expected value is 1

Can someone please help me identifying how to update the nested values correctly from the input box?

7
  • why this value={student.name ?? ""} Commented Aug 5, 2022 at 19:58
  • @sushildlh Just to make sure if there is nothing in student.name. "" will be taken Commented Aug 5, 2022 at 20:09
  • use | not ??. ALso are you updating state or doing direct manipulation on object? Commented Aug 5, 2022 at 20:10
  • @sushildlh Using | did not solved the problem. I am not updating state as of now. I am just trying to get the value and update it on console. Commented Aug 5, 2022 at 20:23
  • The issue remained same Commented Aug 5, 2022 at 20:24

1 Answer 1

1

you must use state to store data

let student = {
    name: null,
    marks: { english: null },
};

change to

const [student, setStudent] = useState({
    name: null,
    marks: { english: null },
});

const handleChange = (updatedValue) => {
  setStudent(student => ({
    ...student,
    ...updatedValue,
    marks: { ...student.marks, ...updatedValue.marks },
  }))
};
Sign up to request clarification or add additional context in comments.

6 Comments

I am not trying to set state as of now. I am just trying to store the values in the student variable from input.
if you want to update ui you must use state
I am not trying to update UI. I am just trying to update the variable
it will fix issue with disappearing from input box, and save student as you expected
IT did not fixed the issue. Please check the sandbox code - codesandbox.io/s/cocky-worker-09ed1z?file=/src/App.js
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.