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

setValue is not defined in react for react hooks

I’m getting an error saying the setValue portion of my code is not defined, but I did in the bottom portion of my main function. Not sure what’s going on.

import React, {useState} from "react"; 

const updateAPI = () => {
  setValue("test"); 
}

export default function App() {
  const [value, setValue] = useState(""); 
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <button onClick={updateAPI}></button>
      <p>{value}</p>
    </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

Restructure code as below and try,

import React, {useState} from "react"; 

export default function App() {
  const updateAPI = () => {
    setValue("test"); 
  }

  const [value, setValue] = useState(""); 
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <button onClick={updateAPI}>HI</button>
      <p>{value}</p>
    </div>
  );
}
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