首页 > 解决方案 > 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"

这是模拟器上显示的错误

标签: javascriptreactjsreact-native

解决方案


推荐阅读