Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

react gives the error: setCount is not a function

receiving error of setcount is not a function.
i’m newbie please help me

import React, { memo, useState } from "react";

export const Container = memo(function Container() {
    const { count, setCount } = useState(0);

  return (
    <div>
      {count}
    <button onClick={()=>setCount(count+1)}>Increase</button>
    </div>
  );
});

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

useState doesn’t return an object which you’re destructuring.
It returns an array of two values i.e. value and a function.

You should read React useState docs

It returns a pair of values: the current state and a function that
updates it. This is why we write const [count, setCount] = useState().
This is similar to this.state.count and this.setState in a class,
except you get them in a pair. – REACT DOCS

Change

const { count, setCount } = useState(0);

to

 const [ count, setCount ] = useState(0);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading