React is a JS Library tool for making User Interface(UI) created and managed by Facebook. React enables us to program by using a declarative programming method and optimize updates while upadating UIs via using Virtual DOM. By using component based programming, we are able to effectively compose complex UI and manage components by using JSX expressions.
React uses declarative programming method which focuses on "What?" rather than "How?" to solve a certain issue.
Vanilla JS uses procedural programming which focuses on how to get to a certain goal while React focuses on the goal. If we declare the goal in React, React will take care of the "How?" part itself.
React uses Vitrual DOM bases on former UI on memory to calculate the [minimal set(최소 집합)] to minimize processing for the best efficiency.
Definition of Component - recyclable group unit of UI
Features of component
- Recyclable when needed
- Usable on independent occasions for effecient code maintenance
- Can comprehend other component
- Better readability
Types of component
Class Component - component which has to include a render() method and returns JSX. Relevant functions usable via state and lifecycle API
// App.js
import React from 'react';
class App extends React.Component {
render() {
return <h1>This is Class Component!</h1>;
}
}
export default App;
Function Component - component which returns JSX without using render() method making it way more simple but unable to use sta†e and lifecycle which made it less useful. However, React 16.8 version added a Hook technique making sta†e usable in function component.
// App.js
import React from 'react';
const App = () => {
return <h1>This is Function Component!</h1>;
};
export default App;
Using components
Component names should always start with upper-cased letters since React interprets lower-cased components to HTML tags.
Definition of JSX - an extended form of JS expression used in react for accessing and implementing HTML and JS logic inside a single file.
Features of JSX - Recognizable since it's similar to HTML tags and could be used to sync HTML markup and JS logic in a single file.
JSX expression
const hi = <p>Hi</p>;
// Greetings.js
import React from 'react';
const Greetings = () => {
const name = '김개발';
return <h1>{name}님, Welcome to Wecode!</h1>;
};
export default Greetings;
/* HTML
<h1 class="greetings">Welcome to Wecode!</h1>
/* JSX
<h1 className="greetings">Welcome to Wecode!</h1>
// JS
const title = document.getElementsByClassName("title")[0];
title.addEventListener("click", handleClick);
// JSX
<h1 className="title" onClick={handleClick}>
Welcome to Wecode!
</h1>
// HTML
<h1 style="color:red;background-image:yellow">Welcome to Wecode!</h1>
// JSX
<h1 style={{ color: "red", backgroundImage: "yellow" }}>
Welcome to Wecode!
</h1>
<img>) in HTML, needs to be closed with / as <img/>. // Bad
const Greetings = () => {
return (
<h1>김개발님!</h1>
<h2>위코드에 오신 걸 환영합니다!</h2>
);
}
// Good
const Greetings = () => {
return (
<div>
<h1>김개발님!</h1>
<h2>위코드에 오신 걸 환영합니다!</h2>
</div>
);
}
<React.Fragment> tag without adding meaningless DOM element. This tag can be also contracted with the following tag <>...</>.const Greetings = () => {
return (
<React.Fragment>
<h1>김개발님!</h1>
<h2>위코드에 오신 걸 환영합니다!</h2>
</React.Fragment>
);
};
or
const Greetings = () => {
return (
<>
<h1>김개발님!</h1>
<h2>위코드에 오신 걸 환영합니다!</h2>
</>
);
};