Demo
- Emotion Diary
- with Javascript, React and VS Code.
Javascript
- CodeSandbox
- https://codesandbox.io/
- You can easily practice Javascript in Web with CodeSandbox.
- Without account, Click Create
Sandbox
. Then you can create your project.
let vs var
- If you want to declare variable one time and let to do not use these name for vatiable, you should use
let
.
CallBack
- When you want give function as an argument, you can use
CallBack
. - It makes your code more flexible.
Function Presentation
- You can create function like a variable.
Object
Loop
- You can use
Object
more flexible withLoop
Truthy and Falshy
- True : Object, not empty string, number, array
- False : undefined, null, empty string
&& and ||
-
&&
: If left value is falshy, then return left value. If left value is turhy, then return right value. -
||
: If left value is falshy, then return right value. If left value is turhy, then return left value.
Destructing Assignment
- You can break your declaration rule in javascript!
Spread
Syncronize and Asyncronize
- Javascript has only single thread.
- But javascript can run various functions at once.
Promise
- You can use CallBack in Asyncronize with Promise.
- You can use
then
andcatch
for result.
Async and Await
-
Async
set a function to asyncronize. -
Await
process a function like syncronize. So, before await line is finished, never other code is ran. - async function return always
Promise
.
API
- Get data with specific url.
-
fetch
call the url but it raw data. So you should change it to json.
JSONPlaceHolder
- https://jsonplaceholder.typicode.com/
- You can use JSONPlaceHolder website to practice.
VS Code
Prettier
- Set indentation automatically in VS Code.
Terminal
- You can open the
Terminal
in VS Code withCtrl
+J
. - You can run javascript file with
node
.
module.exports
-
When you want to use result of the js file in other js file, you can use
module.exports
andrequire
-
calc.js
- index.js
Node.js
Create Package
- You can create the package with
npm init
and run javascript withnpm start
.
Other’s Package
- npmjs : https://www.npmjs.com/
- Website for npm packages.
Download Package
- You can download a package with
npm i
ornpm install
Use Package
- You can use a package with modulename.
React
- Javascript UI Library.
- Use for code diet and virtual DOM.
React Application
- You can create a react application with
npx create-react-app {app name}
in terminal. - Run the application with
npm start
.
export and import
-
In React, you can use a js file in other js file with
export
andimport
. -
MyHeader.js
- App.js
React.Fragment
-
In React, html code should be closed with a root element like
div
,React.Fragment
or<>
. -
App.js
className
- You cannot use
class
in js code becauseclass
is reserved word already in html. -
Instead, you can use
className
. - App.js
Inline Style
-
You can use style in js like a variable.
-
App.js
State
-
State
can change the state of page. - With
useState
, you can set a value on a first value of array and set function to change first value’s state on a second value of array. - So first value of array is a variable and second value of array is a function.
-
You can call the function which change the state with
{}
. - Counter.js
- App.js
Props
-
Props
means property. -
When you want to pass some values to the component, you can use
Props
. - App.js
- Container.js
- Counter.js
React Developer tools
- Eneter Chrome tools and install React Developer tools in your chrome.
- In react pages, you can see component process, context and etc.
Simple Diary
CRUD
useRef
- You can set a html element as a variable or create a dynamic variable in javascript with
useRef
. - With the value in
()
, you can initialize the reference variable.
focus
- If you have a reference of the element, you can set mouse focusing to the element with
current.focus()
.
Date
-
new Date()
create Date object andgetTime()
create the time to milisecond. -
toLocaleString()
change the milisecond to local time.
e.target
- You can get element value and name with
e.target.value
ande.target.name
.
onChange
-
onChange
set the function to change value of the html element.
Key
- When you want to pass an array to the component, you should set
Key
to specific array items. - In Normal, just use index of array items.
Optimization
React.createContext
- In a html, context is child elements of a parent element.
-
React.createContext
create context as a variable. -
Provider
can envelop elements in html. - With
useContext
, you can use Prop value in other js files.
useReducer
-
useReducer
can replaceuseState
and with reduce code lines with switch-case statement. - In left, it is always array. First value is a variable and second value is a function to change value state.
- In right, there is always
useReducer
function. In function parameters, first parameter is a function with switch-case statement to return the changed variable and second parameter is a initial value of the variable.
useEffect
- In React, there is 3 part of life cycle: Mount, Update, Unmount.
-
useEffect
set life cycle of the function. - To use mount time, you should add empty array for second argument.
- To use update time, you should not use second argument.
- To use unmount time, you should return destructing function on mount.
useCallback
-
useCallback
return memoized component to reuse. - You can set life time like an
useEffect
.
React.memo
-
React.memo
return the memoized variable. - This is for skip rerendering when inner elements is not updated.
useMemo
-
useMemo
skips to render when components has same value with previous render. - You can set life time like an
useEffect
.
Code
- App.js
- DiaryEditor.js
- DiaryList.js
- DiaryItem.js
- LifeCycle.js
- App.css
Emotion Diary
localStorage
- Save your diary data in local storage in web.
- To save the data, you need key and value.
- Value can be number, string, object or etc.
-
setItem()
sets your data on web. First value is key which should be a string and second value is the value. -
getItem()
gets your data from web. You can get a data by key string. - When you use object as a value, then you should use
JSON.stringify()
to set andJSON.parse()
to get.
Route
- Set URL rule.
-
path
is a url andelement
is a page to show. -
:id
gets id from the url. -
useParams()
can get the id from url on each page. - When you want to move to specific site, then use
<Link to={}></Link>
.
navigate
-
navigate()
has two values, first is url and second is option. -
{replace: true}
means you cannot go back.
Title
- You can get the title of window with
document.getElementsByTagName("title")[0]
. You should use[0]
, becausegetElementsByTagName
get always an array. - And set your title with
innerHTML
; -
You can set title also in
index.html
. - public/index.html
getFullYear() and getMonth()
-
getFullYear()
gets year for 4-digits. -
getMonth()
gets month but January is 0 and December is 11. So you want to show actual month, add 1 likegetMonth() + 1
.
Google Fonts
- Import fonts with
@import
.
Open Graph
-
og
sets image, site name, description and etc of the web. -
public/index.html
Code
- App.js
- pages/Home.js
- pages/New.js
- pages/Edit.js
- pages/Diary.js
- util/date.js
- util/emotion.js
- components/DiaryEditor.js
- components/DiaryItem.js
- components/DiaryList.js
- components/EmotionItem.js
- components/MyButton.js
- components/MyHeader.js
- App.css
-
components/MyHeader.js
-
components/MyHeader.js
build
-
npm run build
will make a build folder. In the folder, there are some files for build. For instance, js files which is compressed without white space.
Publish
Firebase
- Create Project on console.
- Go to hosting and click start.
- Open command and type
npm install -g firebase-tools
.
- In VS Code terminal, type
firebase login
and allow authorization for firebase.
- Type
firebase init
.
- Create hosting and set the site name on firebase.
- Set the site on firebase.json
Demo
- https://leehuhlee-emotional-diary.web.app/