简体   繁体   English

反应| 功能组件抛出错误useState Object(…)不是函数

[英]React | Functional component throws error useState Object(…) is not a function

I am trying to create a component based on this article: 我正在尝试根据本文创建一个组件:

https://scotch.io/tutorials/5-ways-to-convert-react-class-components-to-functional-components-w-react-hooks#toc-class-with-state-and-componentdidmount https://scotch.io/tutorials/5-ways-to-convert-react-class-components-to-functional-components-w-react-hooks#toc-class-with-state-and-componentdidmount

But simply copy pasting the functional component from the above link throws an error: 但是,仅从上面的链接复制粘贴功能组件会引发错误:

TypeError: Object(...) is not a function
Home
src/components/Home.js:3
  1 | import React, { useState, useEffect } from 'react';
  2 | 
> 3 | function Home() {
  4 | 
  5 |   const [userName, setUsername] = useState('JD');
  6 |   const [firstName, setFirstname] = useState('John');

Full component code: 完整的组件代码:

import React, { useState, useEffect } from 'react';

function Home() {

  const [userName, setUsername] = useState('JD');
  const [firstName, setFirstname] = useState('John');
  const [lastName, setLastname] = useState('Doe');

  useEffect(() => {
    setInterval(() => {
      setUsername('MJ');
      setFirstname('Mary');
      setLastname('Jane');
    }, 5000);
  });

  const logName = () => {
    // do whatever with the names ...
    console.log(this.state.userName);
    console.log(this.state.firstName);
    console.log(this.state.lastName);
  };

  const handleUserNameInput = e => {
    setUsername({ userName: e.target.value });
  };
  const handleFirstNameInput = e => {
    setFirstname({ firstName: e.target.value });
  };
  const handleLastNameInput = e => {
    setLastname({ lastName: e.target.value });
  };

  return (
    <div>
      <h3> The text fields will update in 5 seconds </h3>
      <input
        type="text"
        onChange={handleUserNameInput}
        value={userName}
        placeholder="Your username"
      />
      <input
        type="text"
        onChange={handleFirstNameInput}
        value={firstName}
        placeholder="Your firstname"
      />
      <input
        type="text"
        onChange={handleLastNameInput}
        value={lastName}
        placeholder="Your lastname"
      />
      <button className="btn btn-large right" onClick={logName}>
        {' '}
        Log Names{' '}
      </button>
    </div>
  );
};

export default Home;

Hooks are an upcoming feature that lets you use state and other React features without writing a class. 挂钩是一项即将推出的功能,可让您无需编写类即可使用状态和其他React功能。 They're currently in React v16.8.0-alpha.0. 他们目前在React v16.8.0-alpha.0中。

Please check react version as the hook methods works with v16.8.0 . 请检查react版本,因为hook方法适用于v16.8.0。

Hope this helps, 希望这可以帮助,

Cheers !! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用useState挂钩对功能组件进行反应,立即执行功能 - React functional component with useState hook executes function immediately 使用函数组件中的 useState 反应无效的挂钩调用错误 - React Invalid Hook Call Error with useState In Functional Component 如何在反应中将函数和对象传输到函数组件 - How to transport a function and an object to a functional component in react React useState boolean 问题(功能组件) - React useState boolean issue (functional component) react挂钩中的useState转换为功能组件 - useState in react hooks converting to functional component 反应本机功能组件 useState 不更新 - react native functional component useState not Updating 无法在React中使用“ useState”功能组件 - Unable to use “useState” functional component in React 在功能组件中初始化 Date() 时,React 抛出“无效的钩子错误” - React throws "invalid hook error" when initializing Date() in a functional component 调用 state 更新 function 在 state 更新 ZC1C425268E68384 组件4F 中使用功能状态钩子1 - Calling a state update function within a state update function - useState hook in React Functional component 如何使用 useState 在反应功能组件的循环中正确设置 state - How to correctly set state in a loop in react functional component using useState
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM