1

I am trying to add an item to the array. That array was fetched using an API.

but when I click to add the item, it's not updating.

here is my onChange code

  const changeHandler = e => {
    e.preventDefault();
    const textName = e.target.value;
    setText(textName);
  };

here is my add to array code

  const addNewHandler = e => {
    e.preventDefault();
    const addNewname = {
      id: Math.random().toString(),
      name: text
    };

    console.log(addNewname);
    const apiupdate = [...api];
    // apiupdate.concat(addNewname);
    // console.log(apiupdate);
    setApi(apiupdate.concat(addNewname));
    console.log(api);
  };

here is where I call that function

<TextField
  variant="outlined"
  value={api.Name}
  name="Name"
  onChange={changeHandler}
/>
<Button
  variant="outlined"
  color="primary"
  style={{ margin: "10px" }}
  onClick={addNewHandler}
>
  Add Me
</Button>

https://codesandbox.io/s/suspicious-hooks-swvn0...

Please use this in your local network issue with fetch when using in the sandbox

7
  • In case, if you need the whole code here it is. pastebin.com/uG6XcGzY Commented Mar 1, 2020 at 7:34
  • 1
    Is the issue that console.log(api) shows the old version (unupdated) of api, or does api actually not update when rendered? Commented Mar 1, 2020 at 7:34
  • It shows old version.. Then I tried something, it showed new version, but again not rendering Commented Mar 1, 2020 at 8:26
  • Your code should work, please make a producible example. Commented Mar 1, 2020 at 8:31
  • It is not working... I'll try to add sandbox in a while Commented Mar 1, 2020 at 8:33

1 Answer 1

1

Change your code like this:

const addNewHandler = e => {
    e.preventDefault();
    const addNewname = {
       id: Math.random().toString(),
       nutritionalInformation: {
         calories: 0,
         fat: 0,
         saturatedFat: 0,
         sugars: 0,
         salt: 0
      },
      name: text
    };

    console.log(addNewname);
    const apiupdate = [...api, addNewname];
    // apiupdate.concat(addNewname);
    // console.log(apiupdate);
    setApi(apiupdate);
    console.log(api);
};

[...api, addNewname] add addNewname to end of api array and make a new array.

Also, I set nutritionalInformation when adding new item.

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

5 Comments

Please do refer my full coding. There may be some other things i might have missed
@LogeshP There are some errors in other parts of your code, I changed my answer and check it. you use `nutritionalInformation' but you never set it.
I changed my answer. Please check it. @LogeshP
your code works fine.. but cant we do this without that nutritional information ?
@LogeshP Yes, you can, You should use guard operator before using it in TableCell like this: <TableCell align="justify"> {row.nutritionalInformation && row.nutritionalInformation.calories} </TableCell>

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.