Getting TypeError: undefined is not an object (evaluating '_mathjs.default.fraction'), React-Native

Getting TypeError: undefined is not an object (evaluating '_mathjs.default.fraction'), React-Native

每当我使用 'mathjs'

时都会出现此错误

TypeError: undefined is not an object (evaluating '_mathjs.default.fraction')

基本上我的代码是这样的

import React from 'react';
import {View, Button} from 'react-native';
import mathjs from 'mathjs';
const App = () => {
  return (
    <View>
      <Button
        title="TestButton"
        onPress={() => {
           console.log(mathjs.fraction(1, 2)); // equals to ½
         }}
        />
   </View>
  );
};

export default App;

查看 library's npm page,我认为您需要使用命名导入,而不是默认导入:

import { fraction } from 'mathjs';

命名导出不会显示为默认导出的属性。您可以使用此语法获取所有命名导出:

import * as mathjs from 'mathjs;

尽管除非你真的全部使用它们,否则我不推荐这样做,因为我认为它会击败你的 javascript 捆绑包的 tree-shaking。

有关详细信息,请参阅 import docs