我使用useContext和useReducer来管理应用程序状态。
下面是代码片段:
//ReducerProvider.tsx
type ContextProviderType = {
state: StateInterface,
dispatch: React.Dispatch<ActionInterface>
}
type CreateContextType = ContextProviderType | string; // i am passing to the createContext either the ContextProvide
我在我的next.js应用程序中使用了带有useReducer钩子的上下文Api,当使用contextProvider调用useContext时,它会返回未定义的结果。 下面是我的代码=> contextPage=> import React, { createContext, useReducer, useContext } from 'react';
import * as types from '../constants/userConstants';
// initialized global store
export const qu
我正在开发一个应用程序,我使用的是next.js的前端,我在试图压缩一个对象时遇到了问题,这就是错误的样子。
Argument of type '{ auth: dataObject; }' is not assignable to parameter of type 'string'
所以,当用户登录时,我试图保存一些数据localStorage,据我所知,如果您在那里保存数据,它必须是tring (我真的不知道为什么),所以这就是为什么我使用json.stringify (在我的代码中)。
现在,为了给您提供更多的上下文,让我向您展示我的代码
这是我的上下文
当我运行这个应用程序时,错误消息显示TypeError: undefined is not an object (evaluating '_useContext.state')
这是我的App.js:
import React from 'react'
import { StatusBar } from 'expo-status-bar';
import { useState, useEffect, useContext } from 'react';
import { StyleSheet, Text, View } from
我试图用钩子替换redux存储,使用动作和还原器,我使用createContext创建了全局存储,使用useReducer创建了全局还原器,并试图在下面的文件中使用带有useContext的全局状态对象,这是抛出错误。此外,我还尝试过记录useContext(Store)的返回值,这给出了一个未定义的值。
import React, {useContext, useEffect} from 'react'
import {newAction} from './actions'
import Store from '../../utils/store
所以我尝试使用react context API创建一种类型的存储,我遇到了一个问题,当我使用useContext时,它返回未定义。我的代码是这样的: StateProvider.js import React, { createContext, useContext, useReducer } from "react";
//Needed to track the basket and the user info
//DATA LAYER
export const StateContext = createContext();
// BUILD PROVIDER
ex