简体   繁体   English

React.js 类型错误:模块不是 function

[英]React.js TypeError: Module not a function

Hopefully this is an easy fix and I'm just blind.希望这是一个简单的解决方法,我只是盲目的。 I'm building a geo calculator where we determine distance traveled using latitude and longitude.我正在构建一个地理计算器,我们使用纬度和经度确定行驶距离。 Long story short, the functions for the math has been provided for us and we are to build the app around that.长话短说,我们已经为我们提供了数学函数,我们将围绕它构建应用程序。

As the title states I receive a TypeError once data is put in and a button is pressed and fails on line 36.正如标题所述,一旦输入数据并按下按钮并在第 36 行失败,我就会收到 TypeError。

import React, { useState} from 'react';
import {StyleSheet, Text, View, TextInput} from 'react-native';
import {Button} from 'react-native-elements';
import Calculations from './Calculations';

const Calculate = ({buttonTitle, lat1, lon1, lat2,lon2, distance, bearing}) => {
    const [state, setState] = useState({coordinate: ''});

    const updateStateObject = (vals) =>{
        setState({
            ...state,
            ...vals,
        });
    };
    return (
        <View style={styles.container}>
            <TextInput 
                placeholder = 'Enter the latitude of your starting location.' 
                onChangeText = {(lat1) => updateStateObject({coordinate: lat1})} //or you could do (val) => {setName(val);}
                value = {state.lat1}/>
            <TextInput 
                placeholder = 'Enter the longitude of your starting location.' 
                onChangeText = {(lon1) => updateStateObject({coordinate: lon1})} //or you could do (val) => {setName(val);}
                value = {state.lon1}/>
            <TextInput 
                placeholder = 'Enter the latitude of your end location.' 
                onChangeText = {(lat2) => updateStateObject({coordinate: lat2})} //or you could do (val) => {setName(val);}
                value = {state.lat2}/>
            <TextInput 
                placeholder = 'Enter the longitude of your end location.' 
                onChangeText = {(lon2) => updateStateObject({coordinate: lon2})} //or you could do (val) => {setName(val);}
                value = {state.lon2}/>
            <Button 
            title= {buttonTitle}
            onPress = {() =>{
                dist = Calculations.computeDistance({coordinate: lat1}, {coordinate: lon1}, {coordinate: lat2}, {coordinate: lon2});
                bear = Calculations.computeBearing({coordinate: lat1}, {coordinate: lon1}, {coordinate: lat2}, {coordinate: lon2});
                updateStateObject({distance: `Distance: ${dist}`});
                updateStateObject({bearing: `Bearing: ${bear}`});
            }} />
        </View>
    );
};

const styles = StyleSheet.create({
    container: {
      flex: 1,
      backgroundColor: '#fff',
      alignItems: 'center',
      justifyContent: 'center',
    },
  });
export default Calculate;

It points to the app failing once computeDistance is called and I'm going to assume compute Bearing will also fail at the current state of the app.一旦调用了computeDistance,它就表明应用程序失败,我将假设计算轴承也将在应用程序的当前state 处失败。 I am importing from a component file called Calculations and the functions are like this:我从一个名为 Calculations 的组件文件中导入,函数如下所示:

const Calculations = () =>{
...
  function computeDistance(lat1, lon1, lat2, lon2) {
    console.log(`p1={${lat1},${lon1}} p2={${lat2},${lon2}}`);
    var R = 6371; // km (change this constant to get miles)
    var dLat = ((lat2 - lat1) * Math.PI) / 180;
    var dLon = ((lon2 - lon1) * Math.PI) / 180;
    var a =
      Math.sin(dLat / 2) * Math.sin(dLat / 2) +
      Math.cos((lat1 * Math.PI) / 180) *
        Math.cos((lat2 * Math.PI) / 180) *
        Math.sin(dLon / 2) *
        Math.sin(dLon / 2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    var d = R * c;
    return `${round(d, 3)} km`;
  }
...
}

export default Calculations;

I'm fairly new to JS and especially new to React.js.我对 JS 很陌生,尤其是对 React.js 很陌生。 Any guidance would be appreciated!任何指导将不胜感激!

Edit: this is the error I receive TypeError: _Calculations__WEBPACK_IMPORTED_MODULE_8__.default.computeDistance is not a function编辑:这是我收到的错误 TypeError: _Calculations__WEBPACK_IMPORTED_MODULE_8__.default.computeDistance is not a function

You are declaring Calculations as a function and you cannot access anything within.您将计算声明为 function 并且您无法访问其中的任何内容。 If you want to call the function as it is in your code you can do this to Calculations:如果您想调用代码中的 function,您可以对计算执行以下操作:

const Calculations = () =>{
  ...
}

function computeDistance(lat1, lon1, lat2, lon2) {
  console.log(`p1={${lat1},${lon1}} p2={${lat2},${lon2}}`);
  var R = 6371; // km (change this constant to get miles)
  var dLat = ((lat2 - lat1) * Math.PI) / 180;
  var dLon = ((lon2 - lon1) * Math.PI) / 180;
  var a =
    Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos((lat1 * Math.PI) / 180) *
      Math.cos((lat2 * Math.PI) / 180) *
      Math.sin(dLon / 2) *
      Math.sin(dLon / 2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  var d = R * c;
  return `${round(d, 3)} km`;
}

Calculations.computeDistance = computeDistance;

export default Calculations;

Thank you everyone for your help.感谢大家的帮助。 what I ended up doing was essentially getting rid of Calculation as a function in its entirety and having the functions on the page by itself.我最终做的基本上是完全摆脱了作为 function 的计算,并在页面上单独拥有这些功能。 Then exporting each function that I needed for Calculate importing them individually using然后导出我需要计算的每个 function 使用单独导入它们

import {computeDistance, computeBearing} from './Calculations';

The other ways suggested were incredibly helpful in helping me think through this and simplifying my code!建议的其他方法对帮助我思考和简化代码非常有帮助!

Seems like you have imported Calculations in the wrong way.似乎您以错误的方式导入了Calculations

You should be doing: import { Calculations} from './Calculations';你应该这样做: import { Calculations} from './Calculations';

By importing Calculations with brackets, React will know that you are taking the function instead of the whole thing.通过导入带括号的Calculations ,React 会知道你使用的是 function 而不是整个东西。 In this way, you can export the function out.这样就可以把function导出了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM