反应原生样板的内容 - 无法加载包

时间:2018-01-21 10:37:58

标签: javascript react-native contentful

尝试将Contentful JavaScript SDK与新的React Native应用程序一起使用时出现问题。重新创建的步骤:

monotonically_increasing_id

然后在index.js中:

react-native init MyApp npm install contentful --save

然后构建/运行:import { createClient } from 'contentful';

有了导入线,它会抱怨缺少的http模块。如果我在https://github.com/Khaledgarbaya/ContentfulReactNative-boilerplate使用Khaled的示例样板,那么它可以正常工作。

知道可能导致冲突的原因是什么?

我已尝试过错误消息中的建议,但他们无法解决问题。

Error when including the contentful library

感谢您提供任何帮助/建议

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。我还没有找到任何将内容导入React Native项目的解决方案。我使用的是react-native@0.49.5和contentful@5.0.5。

我的解决方案是在Contentful Delivery API中使用fetch:https://www.contentful.com/developers/docs/references/content-delivery-api/

以下是我的主要查询示例:

fetch(`https://cdn.contentful.com/spaces/${this.space_id}/entries?access_token=${this.access_token}&include=2&content_type=list_group`, {
  method: 'GET',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  }
}).then((res) => {
  if(res.ok && res.status === 200){

    callback(JSON.parse(res._bodyText));

  } else {
    callback(null)
  }

})

它需要更多的解析,但它对基本的东西非常有效。希望这会有所帮助,直到满足或反应本机解决错误。

答案 1 :(得分:1)

您可以导入内容丰富的SDK的浏览器版本,该版本不依赖于http库:

const { createClient } = require('contentful/dist/contentful.browser.min.js')

此解决方案的信用归维护者here之一。