0
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.

useContextを使っていて、Cannot destructure property 'pause' of 'Object(...)(...)' as it is undefined.のエラーがでたとき

Posted at

useContextを使っているときにこんなエラーが出ました。

Cannot destructure property 'pause' of 'Object(...)(...)' as it is undefined.

オブジェクトでのpauseが未定義だと言われてしまいました。解決するには本当にエラー文そのままで、pauseの型を考えて上げればいいと思います。
そこで、pauseがあるところまで戻ってみました。

\\import React,{createContext} from 'react'
export const Store = createContext()

const App = () => {
   const [pause, setPause] = useState(false)
   return(
  ...

     <Store.Provider value={{pause, setPause}}>
        ..........
     </Store.Provider>

  ...
  )

}

そこまで問題がないように見えます。

実は、ここではなくて、importするときにpauseの型が適切ではありませんでした。

import Store from './App'

これを

import {Store} from './App'

にしてあげる解決しました。初歩的過ぎますね。オブジェクトの形でStoreに保存しているのにimportする形はオブジェクトでなかったため怒られていたようです。

日本語の記事がなかったため、書いてみました。

0
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
0
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?