【问题标题】:How to use node/npm package (oxford-dictionary) in react native?如何在本机反应中使用 node/npm 包(牛津词典)?
【发布时间】:2019-01-02 15:07:13
【问题描述】:

我正在尝试在 react-native 中使用 npm oxford-dictionary 包,我安装了 “npm install oxford-dictioary”并在终端上将此文件作为“node app.js”运行并且工作正常,但是,当我尝试在 react native 中使用它不起作用时,谁能帮我解决问题。是否可以仅通过 react-native 使用此 api,或者我必须使用 nodejs 构建后端?

app.js

var Dictionary = require("oxford-dictionary");    
var config = {
 app_id : "-------",
 app_key : "--------",
 source_lang : "en"
};
      
var dict = new Dictionary(config);

var props = {
 word: "stupendous",
};


var oxfordApi = {
getRovers(){
 var lookup = dict.synonyms(props);
 return fetch(lookup).then((res) => res.json());
  }
  }





      import React from 'react';
      import { StyleSheet, Text, View } from 'react-native';
      //import oxfordApi from './oxfordApi';
      import globals from 'node-libs-react-native/globals';
           var Dictionary = require("oxford-dictionary");  
           var config = {
           app_id : "-------",
           app_key : "--------------------------",
        source_lang : "en"
      };
      
       export default class App extends React.Component {


     constructor(props) {
      super(props);
       this.state = {
         rovers: []
          }
        }

     componentWillMount() {
     var dict = new Dictionary(config);
      var lookup = dict.find("awesome");

       lookup.then((res) => {
         this.setState({
           rovers: res.rovers
           })
          });
          }

        render() {
        console.log("Rovers: ", this.state.rovers);
         return (
           <View style={styles.container}>
          <Text>Hello wordl</Text>
          </View>
           );
         }
             }

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

当我运行“run npm ios”时的结果

“node_modules/oxford-dictionary/index.js”中的包试图导入 Node 标准库模块“https”。它失败了,因为 React Native 不包含 Node 标准库。阅读更多https://docs.expo.io/versions/latest/introduction/faq.html#can-i-use-nodejs-packages-with-expo

【问题讨论】:

标签: node.js react-native


【解决方案1】:

不幸的是,您已经正确地确定了您不能将那个 npm 包与 react-native 一起使用,因为它没有 https 模块。

但是,查看package oxford-dictionary 是一个单独的文件,它提供了一些帮助方法来构造对牛津词典 api 的特定网络请求。

根据fetch 重写这些并制作自己的帮助文件并不难。您甚至可以为牛津词典 api 创建自己的 react-native 包并将其发布到 npm。

关于访问牛津词典的 api,这样做应该没有问题,因为它只是网络请求。 fetch 应该可以应付。您将无法使用 oxford-dictionary npm 包来发出请求。

【讨论】:

  • 谢谢,这是个好消息,介意详细说明一下吗?我对高级 JavaScript 非常陌生并且反应原生,我需要在包中的哪个地方使用 fetch?我认为它在哪里处理 html 请求?
  • 您需要在使用https 的任何地方使用fetch。它可能还需要您更新辅助函数,因为它们是为与https 一起使用而设计的。老实说,我会使用那个包作为指导,并根据fetch 编写我需要的函数。只需编写一个发出一种类型请求的函数即可开始,然后您可以抽象代码以重用于不同的请求。
【解决方案2】:

我重新编写了包、方法并将其发布到 npm。 react-native-oxford-dictionary

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-27
    • 2018-11-06
    • 2019-05-22
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    相关资源
    最近更新 更多