我正在尝试在 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)