最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React hooks: 'TypeError: Object is not a function' - Stack Overflow

programmeradmin2浏览0评论

I'm using React version 16.13.1.

I'm getting 'TypeError: Object is not a function'

Here is my code (error message seems to think something is wrong with line 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();
  }

Here is my custom hook:

import { useState } from 'react';

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

  return {
    val,
    setVal,
    bind: {
      val,
      onChange: event => {
        setVal(event.target.value);
      }
    }
  };
};

Thanks in advance for your help.

I'm using React version 16.13.1.

I'm getting 'TypeError: Object is not a function'

Here is my code (error message seems to think something is wrong with line 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();
  }

Here is my custom hook:

import { useState } from 'react';

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

  return {
    val,
    setVal,
    bind: {
      val,
      onChange: event => {
        setVal(event.target.value);
      }
    }
  };
};

Thanks in advance for your help.

Share Improve this question edited Apr 26, 2020 at 16:40 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Apr 26, 2020 at 13:24 JoshuaRDBrownJoshuaRDBrown 3515 silver badges12 bronze badges 2
  • Please post actual code, not pictures of it. – jmargolisvt Commented Apr 26, 2020 at 13:27
  • 1 Names of custom hooks should start with use, see reactjs/docs/hooks-custom.html#extracting-a-custom-hook – rrebase Commented Apr 26, 2020 at 13:29
Add a ment  | 

1 Answer 1

Reset to default 6

ProcessInput is returning an object, but you are destructuring it to an array.

Try this:

const {val, bind} = ProcessInput('');
发布评论

评论列表(0)

  1. 暂无评论