1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【React】TypeError: Object(...) is not a functionの解決方法

Posted at

症状

ReactでuseHistoryを実装しようとしたところ、下記エラーが発生しました。 翻訳すると、「functionがない」とみたいです。
エラー
Uncaught TypeError: Object(...) is not a function
    at UserDetail (UserDetail.jsx:43)
    at renderWithHooks (react-dom.development.js:14985)
    at mountIndeterminateComponent (react-dom.development.js:17811)
    at beginWork (react-dom.development.js:19049)
    at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
    at invokeGuardedCallback (react-dom.development.js:4056)
    at beginWork$1 (react-dom.development.js:23964)
    at performUnitOfWork (react-dom.development.js:22776)
    at workLoopSync (react-dom.development.js:22707)
    at renderRootSync (react-dom.development.js:22670)
    at performSyncWorkOnRoot (react-dom.development.js:22293)
    at react-dom.development.js:11327
    at unstable_runWithPriority (scheduler.development.js:468)
    at runWithPriority$1 (react-dom.development.js:11276)
    at flushSyncCallbackQueueImpl (react-dom.development.js:11322)
    at flushSyncCallbackQueue (react-dom.development.js:11309)
    at flushSync (react-dom.development.js:22467)
    at Object.scheduleRefresh (react-dom.development.js:24429)
    at react-refresh-runtime.development.js:304
    at Set.forEach (<anonymous>)
    at Object.performReactRefresh (react-refresh-runtime.development.js:293)
    at RefreshUtils.js:62
UserDetail.jsx
import React, {Fragment,useReducer,useEffect,useHistory} from 'react';

export const UserDetail = () => { 
  const history = useHistory();

  function onClickEditHandle() {
    history.push(userEditHistory(state.user.id))
  }

  return (
    <button onClick={onClickEditHandle}/>
  )
}

解決策

useHistoryのimportのfromで指定が間違っていたため起きていたようです。

引っ張ってくる大元に該当のfunctionがなかったため、Object(...) is not a functionと言われていたようです。

import React, {Fragment,useReducer,useEffect} from 'react'; 
import {useHistory} from "react-router-dom";
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?