From e4a301536d30690c1adf533cf8291faaef9ddd9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rald=20L?= Date: Tue, 1 Sep 2020 19:00:42 +0200 Subject: [PATCH] Add a component to render a markdown file from url --- .../Screens/MarkdownRenderScreen.js | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/components/Screens/MarkdownRenderScreen.js diff --git a/src/components/Screens/MarkdownRenderScreen.js b/src/components/Screens/MarkdownRenderScreen.js new file mode 100644 index 0000000..3ba65fc --- /dev/null +++ b/src/components/Screens/MarkdownRenderScreen.js @@ -0,0 +1,47 @@ +import React from 'react'; +import Markdown from "react-native-markdown-display"; +import {withTheme} from "react-native-paper"; +/** + * Read a text file and output the content + * + * Example Usage: + * var myTxt = require("./myTxt.txt"); + * ... + * + */ +class TextFileReader extends React.Component { + constructor(props) { + super(props); + this.state = { + text: "" + }; + } + + componentDidMount() { + this.readTextFile(this.props.text); + } + + readTextFile = file => { + let xhr = new XMLHttpRequest(); + xhr.open("GET", file, true); + xhr.onreadystatechange = () => { + if(xhr.readyState === 4 && xhr.status === 200){ + const allText = xhr.responseText; + this.setState({ + text: allText + }); + } + }; + xhr.send(null); + }; + + render(){ + return ( + {this.state.text} + ) + } +} + +export default withTheme(TextFileReader); \ No newline at end of file