Skip to content

Commit b504d16

Browse files
committedApr 26, 2020
🍪 Caches the user input markdown to localstorage
1 parent c91371a commit b504d16

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "markdown-editor",
3-
"version": "1.3.1",
3+
"version": "1.4.0",
44
"description": "A react app to preview and edit Markdown",
55
"private": true,
66
"author": {

‎src/components/MarkdownEdit.jsx

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
import React from "react";
1+
import React, { useEffect, useState } from "react";
22
import Snackbar from "@material-ui/core/Snackbar";
33
import { Alert, AlertTitle } from "@material-ui/lab";
4+
import placeholder from "../data/placeholder";
45

56
function MarkdownEdit({ content, changeContent }) {
6-
const [open, setOpen] = React.useState(false);
7+
const [open, setOpen] = useState(false);
8+
9+
useEffect(() => {
10+
if(content===''){
11+
localStorage.setItem("markdown",placeholder);
12+
}
13+
else{
14+
localStorage.setItem("markdown",content);
15+
}
16+
}, [content])
717

818
const handleEditorChange = (event) => {
919
event.preventDefault();

‎src/components/WorkArea.jsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import React, { useState, useEffect } from "react";
22
import Split from "react-split";
33
import MarkdownEdit from "./MarkdownEdit";
44
import MarkdownPreview from "./MarkdownPreview";
5-
import placeholder from '../data/placeholder'
5+
import placeholder from "../data/placeholder";
66

77
function WorkArea() {
8-
const [markDown, setMarkDown] = useState(placeholder);
8+
let markdown = localStorage.getItem("markdown") || placeholder;
9+
const [markDown, setMarkDown] = useState(markdown);
910
const [orientation, setOrientation] = useState("horizontal");
1011

1112
useEffect(() => {

0 commit comments

Comments
 (0)