前提・実現したいこと
React(Next.js),TypeScript,Firebaseでタイマーアプリを作っています。
ログイン画面でGoogle認証機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
npm run devで実行
ターミナル出力
Error: could not find react-redux context value; please ensure the component is wrapped in a <Provider> at useReduxContext (C:\Users\sekai\pomodoro-clock\node_modules\react-redux\lib\hooks\useReduxContext.js:30:11) at useSelector (C:\Users\sekai\pomodoro-clock\node_modules\react-redux\lib\hooks\useSelector.js:133:28) at Home (C:\Users\sekai\pomodoro-clock.next\server\pages\index.js:447:72) at processChild (C:\Users\sekai\pomodoro-clock\node_modules\react-dom\cjs\react-dom-server.node.development.js:3353:14) at resolve (C:\Users\sekai\pomodoro-clock\node_modules\react-dom\cjs\react-dom-server.node.development.js:3270:5) at ReactDOMServerRenderer.render (C:\Users\sekai\pomodoro-clock\node_modules\react-dom\cjs\react-dom-server.node.development.js:3753:22) at ReactDOMServerRenderer.read (C:\Users\sekai\pomodoro-clock\node_modules\react-dom\cjs\react-dom-server.node.development.js:3690:29) at renderToString (C:\Users\sekai\pomodoro-clock\node_modules\react-dom\cjs\react-dom-server.node.development.js:4298:27) at Object.renderPage (C:\Users\sekai\pomodoro-clock\node_modules\next\dist\next-server\server\render.js:54:854) at Function.getInitialProps (C:\Users\sekai\pomodoro-clock.next\server\vendors-node_modules_next_dist_pages__document_js.js:627:19)
該当のソースコード
React
1import * as React from 'react'; 2import { useState, useEffect } from 'react'; 3import dynamic from 'next/dynamic'; 4import firebase from 'util/firebase'; 5import CategoryList from 'components/CategoryList'; 6import Header from 'components/Header'; 7import Calendar from 'components/Calendar'; 8import { useSelector, useDispatch } from 'react-redux'; 9import { selectUser, login, logout } from 'features/userSlice'; 10import { auth } from '../../lib/db'; 11import Auth from 'components/Auth'; 12 13//ApexCharts読み込むのにNext.jsで必要な設定 14const DynamicGraphComponentWithNoSSR = dynamic(() => import('../views/Graph/Graph'), { ssr: false }); 15 16const Home: React.FC = (props) => { 17 const [categories, setCategories] = useState([]); 18 //HomeComponentからredux/userを取得してローカルのuserに渡す 19 const user = useSelector(selectUser); 20 //login, logoutアクションを送信(dispatch)する 21 const dispatch = useDispatch(); 22 23 useEffect(() => { 24 //firebaseのauth管理下のuser.stateに変化があれば毎回呼び出す 25 const unSub = auth.onAuthStateChanged((authUser) => { 26 if (authUser) { 27 dispatch( 28 //useSlice.loginをdispatch 29 login({ 30 uid: authUser.uid, 31 photoUrl: authUser.photoURL, 32 displayName: authUser.displayName, 33 }), 34 ); 35 } else { 36 //useSlice.logoutをdispatch 37 dispatch(logout()); 38 } 39 }); 40 return () => { 41 unSub(); 42 }; 43 }, [dispatch]); 44 45 useEffect(() => { 46 async function fetchData() { 47 const userId = '12345678'; //テストId 48 await Promise.all([getUser(userId), getCategories(userId)]); 49 } 50 fetchData(); 51 }, []); 52 53 const getUser = async (userId: any) => { 54 try { 55 const user = await firebase.getUserData(userId); 56 } catch (e) { 57 console.error(e); 58 } 59 }; 60 61 const getCategories = async (userId: any) => { 62 try { 63 const categories = await firebase.getCategories(userId); 64 setCategories(categories); 65 } catch (e) { 66 console.error(e); 67 } 68 }; 69 70 //{user.uid ? (ユーザが存在する時の処理) :(ユーザが存在しない時の処理)} 71 return ( 72 <> 73 74 <Header title="タイマー" /> 75 <div className="content"> 76 <div style={{ marginBottom: 40 }}> 77 <p className="title">カテゴリー</p> 78 <CategoryList categories={categories} /> 79 </div> 80 <div style={{ marginBottom: 40 }}> 81 <p className="title">作業時間</p> 82 <React.StrictMode> 83 <DynamicGraphComponentWithNoSSR /> 84 </React.StrictMode> 85 </div> 86 <div style={{ marginBottom: 40 }}> 87 <p className="title">カレンダー</p> 88 <Calendar /> 89 </div> 90 </div> 91 92 <Auth /> 93 94 </> 95 ); 96}; 97 98export default Home;
store
export const store = configureStore({ reducer: { user: userReducer, }, });
action
export const userSlice = createSlice({ name: 'user', initialState:{ user: {uid:"", displayName: ""}, }, reducers: { login: (state, action) => { state.user = action.payload; }, logout: (state) => { state.user = {uid:"", displayName: ""}; }, //USER型のオブジェクトをreact componentからdispatchする際に受け取る updateUserProfile: (state, action: PayloadAction<USER>) => { //payload/displayNameをRedux/userステート/displayName属性に更新 state.user.displayName = action.payload.displayName; state.user.photoUrl = action.payload.photoUrl; }, }, });
試したこと
エラー内容から、Provider内にコンポーネントを格納する必要があるとのことですが、
具体的な手順が不明で手が付けられないです。
ソリューションリスト
- https://stackoverflow.com/questions/60329421/usedispatch-error-could-not-find-react-redux-context-value-please-ensure-the
- https://qiita.com/r_1105/items/ddcb1ac56df13a0c77ca
- https://github.com/reduxjs/react-redux/issues/1489
- https://www.codegrepper.com/code-examples/javascript/could+not+find+react-redux+context+value%3B+please+ensure+the+component+is+wrapped+in+a+%3CProvider%3E+testing
- http://5.9.10.113/66164315/react-enzyme-test-could-not-find-react-redux-context-value-please-ensure-the-c
- https://www.fixes.pub/program/786985.html
補足情報(FW/ツールのバージョンなど)
"react-redux": "version": "7.2.4",
"react-dom": "version": "17.0.2",
回答1件
あなたの回答
tips
プレビュー