PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, December 14, 2022

[FIXED] Why does useState work with export default function but not class extends React.Component

 December 14, 2022     javascript, reactjs, syntax, use-state     No comments   

Issue

Let's say I have a function called Login

Why does the following syntax work with the useState

export default function Login(){

    const [user, setUser] = useState({username: '', password: ''})

But when you use class Login extends React.Component, an error is given? Like this

class Sidebar extends React.Component {

  const [user, setUser] = useState({username: '', password: ''})

  render () {
      ....

Solution

You can read the React documentation here. React Hooks are a new paradigm and are intended to replace Classes and its logic, so this makes no sense to use hooks inside a class, that’s why it does not work.



Answered By - Rémi Mondenx
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing