javascript - TypeError:未定义不是对象评估('_useContext.add_location')
问题描述
我是 react native 的新手,我的应用程序出现错误。我正在尝试制作地图以获取用户位置并使用上下文和提供程序跟踪位置,并在渲染地图时出错。TypeError:Undefined not an object evaluation('_useContext.add_location') 这是我的 LocationContext.js 文件。
import createDataContext from "./createDataContext";
const LocationContext = (state, action)=>{
switch (action.type) {
case 'add_current_location':
return {...state, currentLocation: action.payload};
default:
return state;
}
};
const add_Location = dispatch => (location) =>{
dispatch({type:'add_current_location', payload: location})
};
const startRecording = dispatch => () => {
};
const stopRecording= dispatch =>()=>{
};
export const {Context, Provider}= createDataContext( LocationContext,
{startRecording, stopRecording, add_Location },
{recording: false, location:[],currentLocation:null}
);
这是我的 TrackCreateScreen.js
//import '../_mockLocation';
import React, {useEffect, useState, useContext} from 'react';
import {StyleSheet} from 'react-native';
import {Text} from "react-native-elements";
import {SafeAreaView} from "react-native";
import Map from "../components/Map";
import {requestPermissionsAsync, watchPositionAsync, Accuracy} from "expo-location";
import { Context as LocationContext } from "../context/LocationContext";
const TrackCreateScreen = () => {
const { add_Location } = useContext(LocationContext);
const [err, setErr] = useState(null);
const startWatching = async () => {
try {
await requestPermissionsAsync();
await watchPositionAsync({
accuracy: Accuracy.BestForNavigation,
timeInterval: 1000,
distanceInterval: 10
}, location => { add_Location(location)
});
} catch (e) {
setErr(e);
}
};
useEffect(() => {
startWatching();
}, []);
return (
<SafeAreaView forceInset={{top: 'always'}}>
<Text h3>TrackCreate Screen</Text>
<Map/>
{err ? <Text>Please enable services</Text> : null}
</SafeAreaView>
);
};
const styles = StyleSheet.create({
textStyle:
{
fontSize: 48
}
});
export default TrackCreateScreen;
这是我的依赖项。
"expo": "~36.0.0",
"expo-location": "~8.0.0",
"ngrok": "^3.2.7",
"react": "~16.9.0",
"react-dom": "~16.9.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-36.0.0.tar.gz",
"react-native-elements": "^1.2.7",
"react-native-gesture-handler": "~1.5.0",
"react-native-maps": "0.26.1",
"react-native-reanimated": "~1.4.0",
"react-native-version-check": "^3.3.1",
"react-native-web": "~0.11.7",
"react-navigation-stack": "^1.10.3"
解决方案
推荐阅读
- python - 如何在python中比较日期?我有月份日期年份格式并想与当前日期(7 天内)进行比较
- machine-learning - 神经网络 - 具有空输入的额外类的分类器
- sql - Flask 和 sqlite3 - 在“插入”sql 命令可以工作之前,是否需要用数据填充表?
- r - 有没有办法在 igraph R 中为输入和输出单独加权边缘?
- node.js - 使用条带向后端发出 http 请求时遇到问题
- bash - 无法在 macOS Catalina 10.15 上通过 mktemp 转换为临时文件
- firebase - 当我尝试显示当前 Firebase 用户的电子邮件时,为什么会得到 null?
- mongoose - 仅在超测的一项测试中连接 ECONNREFUSED 127.0.0.1:80 错误
- flutter - 如何使非固定大小的父行/列填满整个空间
- java - Javadoc 未生成 -- doc 文件夹