感谢 @Filipe 的回复,我得到了一些指导,并得到了一个适合您需求的工作示例。
就我而言,我有一个.md
文件上的assets/markdown/
文件夹,该文件名为test-1.md
诀窍是找一个当地人url
对于该文件,然后使用fetch
API 来获取其内容string
.
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Markdown from 'react-native-markdown-renderer';
const copy = `# h1 Heading 8-)
| Option | Description |
| ------ | ----------- |
| data | path to data files to supply the data that will be passed into templates. |
| engine | engine to be used for processing templates. Handlebars is the default. |
| ext | extension to be used for dest files. |
`;
export default class App extends React.Component {
constructor(props) {
super(props)
this.state = {
copy: copy
}
}
componentDidMount() {
this.fetchLocalFile();
}
fetchLocalFile = async () => {
let file = Expo.Asset.fromModule(require("./assets/markdown/test-1.md"))
await file.downloadAsync() // Optional, saves file into cache
file = await fetch(file.uri)
file = await file.text()
this.setState({copy: file});
}
render() {
return (
<Markdown>{this.state.copy}</Markdown>
);
}
}
编辑:为了消除错误
无法从“App.js”解析“./assets/markdown/test-1.md”
你需要添加packagerOpts
@Filipe 的片段的一部分到您的app.json
file.
app.json
{
"expo": {
...
"assetBundlePatterns": [
"**/*"
],
"packagerOpts": {
"assetExts": ["md"]
},
...
}
}
编辑2:
回答@Norfeldt 的评论:
虽然我用react-native init
在做我自己的项目时,我对世博会不太熟悉,我得到了这个世博会小吃,它可能会给你一些答案:https://snack.expo.io/Hk8Ghxoqm https://snack.expo.io/Hk8Ghxoqm.
由于读取非 JSON 文件时出现问题,它不适用于博览会小吃,但如果您愿意,可以在本地进行测试。
Using file.downloadAsync()
将阻止应用程序对在该应用程序会话中托管文件的服务器进行 XHR 调用(只要用户不关闭并重新打开应用程序)。
如果您更改文件或修改文件(通过调用来模拟Expo.FileSystem.writeAsStringAsync()
),只要您的组件重新渲染并重新下载文件,它就应该显示更新的内容。
每次您的应用程序关闭并重新打开时都会发生这种情况,因为file.localUri
就我而言,不会在每个会话中持续存在,因此您的应用程序将始终调用file.downloadAsync()
每次打开时至少一次。因此,显示更新的文件应该没有问题。
我也花了一些时间测试一下使用速度fetch
与使用Expo.FileSystem.readAsStringAsync()
,而且它们的平均数是相同的。很多时候Expo.FileSystem.readAsStringAsync
快了约 200 毫秒,但我认为这并不是一个大问题。
我创建了三种不同的方法来获取同一文件。
export default class MarkdownRenderer extends React.Component {
constructor(props) {
super(props)
this.state = {
copy: ""
}
}
componentDidMount() {
this.fetch()
}
fetch = () => {
if (this.state.copy) {
// Clear current state, then refetch data
this.setState({copy: ""}, this.fetch)
return;
}
let asset = Expo.Asset.fromModule(md)
const id = Math.floor(Math.random() * 100) % 40;
console.log(`[${id}] Started fetching data`, asset.localUri)
let start = new Date(), end;
const save = (res) => {
this.setState({copy: res})
let end = new Date();
console.info(`[${id}] Completed fetching data in ${(end - start) / 1000} seconds`)
}
// Using Expo.FileSystem.readAsStringAsync.
// Makes it a single asynchronous call, but must always use localUri
// Therefore, downloadAsync is required
let method1 = () => {
if (!asset.localUri) {
asset.downloadAsync().then(()=>{
Expo.FileSystem.readAsStringAsync(asset.localUri).then(save)
})
} else {
Expo.FileSystem.readAsStringAsync(asset.localUri).then(save)
}
}
// Use fetch ensuring the usage of a localUri
let method2 = () => {
if (!asset.localUri) {
asset.downloadAsync().then(()=>{
fetch(asset.localUri).then(res => res.text()).then(save)
})
} else {
fetch(asset.localUri).then(res => res.text()).then(save)
}
}
// Use fetch but using `asset.uri` (not the local file)
let method3 = () => {
fetch(asset.uri).then(res => res.text()).then(save)
}
// method1()
// method2()
method3()
}
changeText = () => {
let asset = Expo.Asset.fromModule(md)
Expo.FileSystem.writeAsStringAsync(asset.localUri, "Hello World");
}
render() {
return (
<ScrollView style={{maxHeight: "90%"}}>
<Button onPress={this.fetch} title="Refetch"/>
<Button onPress={this.changeText} title="Change Text"/>
<Markdown>{this.state.copy}</Markdown>
</ScrollView>
);
}
}
只需交替使用这三者即可查看日志中的差异。