Hello YouTube, this is Mary from Happy Coder.
In today's video, I'm going to show you how to build a Markdown previewer web app,
which is the first React project from Free Code Camp.
Here on the screen is the final product.
On the left side, it's the editor in which you can type in the content and apply the
markdown syntax to it, on the right side, the rendered text will be displayed simultaneously.
As usual, in the description box, I have added a link to a copy of my codes in a Google Drive
shared folder.
Please feel free to download and examine the codes.
Since this is a React project, of course I'm going to use the react library.
As you may have already known, there are a lot of courses out there instructing how to
install the react library using npm and it requires a lot of configurations.
The app we are building today is rather simple, so I chose to add the CDN links of React and
React DOM from Facebook to this script to save us time on the installation and configuration
process.
I also added the Markdown library via CDN and added its link to the script.
The last thing I added to the script is the CDN link to Babel, which will transcribe my
ECMA6 scripts to ECMA5 so that my app can run on all the popular browsers.
Now we have all the libraries added.
Let's go have a look at my DOM structure which only contains one div tag with the id
"App", as using React allows us to update the DOM dynamically by writing html in JavaScript.
I will explain in details how this is achieved soon.
A very important concept in React is "state".
By updating the state of our components, the DOM will be re-rendered to reflect the change
almost instantaneously.
This means if we link the user's input to the state, we will see the change shown on
the screen as the user is making the entry.
Here is my code of how this idea is realised.
I built a React component called "Editor", it will be called in the ReactDom.render method
at the end of the script and added as a class to the id "app" built in the original
html.
In this React component, I first added a constructor and assigned the initial state "editor"
which contains a key pair: the property is called "content" and the value is defaulted
to a string of a bunch of texts with markdown.
A note here is that using ES6, the "super" method must be used when a constructor is
defined.
Then I built a function called rawMarkup, in which the marked method from the markdown
library we have added via CDN is called to render the strings in the "state" with
proper markdown syntax.
Next, I built another function called "onChange" and it takes one parameter "event".
This function is to be called when the user enters any input.
In this function, I used setState method to update the default value I entered previously
with the user's input.
One thing to note here is that a binding method must be used to make 'this' work in the
callback function, unless you're using the arrow function syntax.
Please refer to Facebook's React documentation concerning this issue.
Once we have the state set up and all the required functions built, it's time to update
the DOM with our desired values.
In the render function, I wrote some html codes to specify what I want to see in the
DOM.
There are 2 things which are worth mentioning.
The first thing is that to take the user's input, the textarea tag should be used instead
of the input tag, as the textarea allows the user to enter multiple lines of texts which
input doesn't.
Secondly, to return the markdown values, I used dangerouslySetInnerHTML and passed the
object "rawMarkup" with a __html key.
This is recommended in the Facebook React reference, again please refer to the official
Facebook react documentation concerning this practice.
This wraps up today's video.
Hope it's been informative.
Please comment, like, share and subscribe to my channel.
See you in my next video.

Không có nhận xét nào:
Đăng nhận xét