React hooks:“类型错误:对象不是函数”

Jos*_*own 0 javascript reactjs react-hooks

我使用的是 React 版本 16.13.1。

我收到“类型错误:对象不是函数”

这是我的代码(错误消息似乎认为第 7 行有问题):

import React, { useState } from 'react';
import fb from '../config/firebase';
import ProcessInput from './customHooks/processInput';

const DashBoard = ({ level, newUser }) => {

  const [val, bind] = ProcessInput('');

  const handleChange = (e) => {
    e.preventDefault();
  }
Run Code Online (Sandbox Code Playgroud)

这是我的自定义钩子:

import { useState } from 'react';

export const ProcessInput = value => {
  const [val, setVal] = useState(value);

  return {
    val,
    setVal,
    bind: {
      val,
      onChange: event => {
        setVal(event.target.value);
      }
    }
  };
};
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助。

Her*_*rab 6

ProcessInput 返回一个对象,但您将其解构为数组。

尝试这个:

const {val, bind} = ProcessInput('');
Run Code Online (Sandbox Code Playgroud)