0

I'm struggling to find a way to update and manipulate the fetched data. I want the {data.servings} to increase by 1 every time I click the Plus button. Even though the rendered data is a number of servings but it's data type is STRING so I figured a way to assign its value to a variable called portions and convert it to number like below. However, I can't update or manipulate it. Any helps would be appreciated. Thanks in advance!!

const handleData = () => {

  const [data, setData] = useState();

    useEffect(()=>{
      fetch(url)
        .then(res => res.json())
        .then(data => setData())
    },[url])


    const handleIncrement = (e) =>{
      let portions = parseInt(e.currentTarget.previousSibling.innterText)
    }
    return (
      <>
        <Minus />
        //the output of {data.servings} is 2 but it's data type is STRING
        <p>{data.servings}</p>
        <Plus onClick={handleIncrement}/>
      </>
    )
}

1 Answer 1

1

You can try out this handleIncrement function

const handleIncrement = () => {
  let copyData = { ...data };
  copyData.servings = `${parseInt(copyData.servings) + 1}`;
  setData(copyData);
};
Sign up to request clarification or add additional context in comments.

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.