1

I have this code.

<Grid item md={4} sm={2} xs={12}>
            <TextField
              label="Qty"
              name="qty"
              id="qty"
              variant="filled"
              type="number"
              fullWidth
              onChange={itemChange}
              value={item.qty}
            ></TextField>
          </Grid>
          <Grid item md={4} sm={2} xs={12}>
            <TextField
              label="Unit price"
              name="unitPrice"
              variant="filled"
              type="number"
              fullWidth
              onChange={itemChange}
              value={item.unitPrice}
            ></TextField>
          </Grid>

In my itemChange method, I console log the e.target

 const itemChange = async (e) => {
    console.log("e", e.target);
    setItem({ ...item, [e.target.name]: [e.target.value] });
  };

I got this

enter image description here

But I expect to get an object. So then I can use target.value or target.name.

Why could this happen?

2
  • I don't see anything unexpected here. e.target is the element that triggered the event (in this case, the input). What were you expecting it to be? Commented Sep 29, 2020 at 0:37
  • 2
    to acess the value returned from onChange of TextField from MUI you should acess it like in most cases. e.target.value. Where target is a DOM node that triggered the event and value is the actual value captured by the onChange event Commented Sep 29, 2020 at 0:43

2 Answers 2

3

It is an object. But it is displayed like this in the console because it's a DOM element. You can right-click on the console where it says <input ... and choose "Store as global variable" in the context menu. That will store e.target as temp1. Then you can try different things like temp1.value and temp1.name in the console.

Sign up to request clarification or add additional context in comments.

Comments

2

Perhaps you are simply confused because of the way the browser prints the object. This is certainly an object, so you can add .value or .name onto the end of e.target._____ and get your result.

You can confirm this is an object if you add to your itemChange function:

console.log(typeof e.target);
// or even this:
console.log(e.target.toString())

Don't forget in Javascript, the following are the only things that are not objects:

  • null
  • undefined
  • strings
  • numbers
  • boolean
  • symbols

Yet even then, they have their counterparts that are objects.

E.g String is an object wrapper for a string type. An object that will represent (and allow you to alter) a sequence of characters.

Comments

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.