相关疑难解决方法(0)

在 React Native 中使用 useContext 的 Context API 显示 TypeError

我正在尝试在 React-Native 中使用现代 Context API,但出现以下错误:

类型错误:类型错误:未定义不是对象(评估 'Context._context')

我的 createDataContext.js:

import React, { useReducer } from 'react'

export default (reducer, actions, defaultValue) => {
    const Context = React.createContext()

    const Provider = ({ children }) => {
        const [state, dispatch] = useReducer(reducer, defaultValue)

        const boundActions = {}
        for (let key in actions) {
            boundActions[key] = actions[key](dispatch)
        }

        return (
            <Context.Provider value={{ state, ...boundActions }}>
                {children}
            </Context.Provider>
        )
    }

    return { Context, Provider }
}
Run Code Online (Sandbox Code Playgroud)

我的context.js:

import { AsyncStorage } from 'react-native'
import …
Run Code Online (Sandbox Code Playgroud)

reactjs react-native expo react-hooks

5
推荐指数
1
解决办法
8449
查看次数

标签 统计

expo ×1

react-hooks ×1

react-native ×1

reactjs ×1