생활코딩 리액트
리액트의 핵심적인 역할은 사용자 정의 태그를 만드는 것
사용자 정의 태그는 부품으로 사용된다
리액트를 만드는 방법은 class와 function(함수)가 있지만
최근에는 함수를 많이 사용한다.
https://ko.reactjs.org/docs/create-a-new-react-app.html
추천 툴체인 - React를 배우고 있거나 아니면
새로운 싱글 페이지 앱을 만들고 싶다면 Create React App.
설치
참고 사이트 https://create-react-app.dev/
현재 디렉토리를 가르키는 .을 사용해 설치
npx create-react-app .
npm 명령어
npm start
개발 모드로 프로그램 실행
npm run build
실제 배포 모드
npm test
테스트 모드
npm run eject
내부 설정 파일을 꺼내는 역할
수정
src > index.js (입구)
리액트를 구동시키면 index.js 에 적혀있는 대로 동작하게 된다
import App from './App';
//.js 파일 확장자명이 생략된 것
<App />
= App.js 파일로 부터 온 태그
App.js 로 UI 구성 후 App.css
import './index.css'; = src > index.css
document.getElementById('root') = public > index.html
배포
기존 터미널을 끄고
빌드 명령 npm run build
npx serve -s build
컴포넌트
사용자 정의태그 = 컴포넌트
기본태그 : 소문자
컴포넌트 : 첫 글자 대문자
props
기존 html 태그는 속성을 가지고 있어 입력 값을 가질 수 있다
리액트 속성을 props라고 부른다
어떻게 우리가 만든 컴포넌트에 props을 장착할 수 있을까?
기존 App.js
import logo from './logo.svg';
import './App.css';
function Header(){
return <header>
<h1><a href="/">WEB</a></h1>
</header>
}
function Nav(){
return <nav>
<ol>
<li><a href="/read/1">html</a></li>
<li><a href="/read/2">css</a></li>
<li><a href="/read/3">js</a></li>
</ol>
</nav>
}
function Article(){
return <article>
<h2>Welcome</h2>
Hello, WEB
</article>
}
function App() {
return (
<div>
<Header></Header>
<Nav></Nav>
<Article></Article>
</div>
);
}
export default App;
props 적용한 App.js
function Header(props) {
console.log("props", props, props.title);
return (
<header>
<h1>
<a href="/">{props.title}</a>
</h1>
</header>
);
}
function Nav(props) {
const lis = [];
for (let i = 0; i < props.topics.length; i++) {
let t = props.topics[i];
lis.push(
<li key={t.id}>
<a href={"/read/" + t.id}>{t.title}</a>
</li>
);
}
return (
<nav>
<ol>{lis}</ol>
</nav>
);
}
function Article(props) {
return (
<article>
<h2>{props.title}</h2>
{props.body}
</article>
);
}
function App() {
const topics = [
{ id: 1, title: "html", body: "html is..." },
{ id: 2, title: "css", body: "cssl is..." },
{ id: 3, title: "javascript", body: "javascript is..." },
];
return (
<div className="App">
<Header title="REACT"></Header>
<Nav topics={topics}></Nav>
<Article title="Welcome" body="Hello, WEB"></Article>
<Article title="Hi" body="Hello, React"></Article>
</div>
);
}
export default App;
Event
import "./App.css";
function Header(props) {
console.log("props", props, props.title);
return (
<header>
<h1>
<a
href="/"
onClick={(event) => {
event.preventDefault();
props.onChangedMode();
}}
>
{props.title}
</a>
</h1>
</header>
);
}
function Nav(props) {
const lis = [];
for (let i = 0; i < props.topics.length; i++) {
let t = props.topics[i];
lis.push(
<li key={t.id}>
<a
id={t.id}
href={"/read/" + t.id}
onClick={(event) => {
event.preventDefault();
props.onChangedMode(event.target.id);
}}
>
{t.title}
</a>
</li>
);
}
return (
<nav>
<ol>{lis}</ol>
</nav>
);
}
function App() {
const topics = [
{ id: 1, title: "html", body: "html is..." },
{ id: 2, title: "css", body: "cssl is..." },
{ id: 3, title: "javascript", body: "javascript is..." },
];
return (
<div className="App">
<Header
title="REACT"
onChangedMode={() => {
alert("Header");
}}
></Header>
<Nav
topics={topics}
onChangedMode={(id) => {
alert(id);
}}
></Nav>
</div>
);
}
export default App;
State
import "./App.css";
import { useState } from "react";
function Header(props) {
console.log("props", props, props.title);
return (
<header>
<h1>
<a
href="/"
onClick={(event) => {
event.preventDefault();
props.onChangedMode();
}}
>
{props.title}
</a>
</h1>
</header>
);
}
function Nav(props) {
const lis = [];
for (let i = 0; i < props.topics.length; i++) {
let t = props.topics[i];
lis.push(
<li key={t.id}>
<a
id={t.id}
href={"/read/" + t.id}
onClick={(event) => {
event.preventDefault();
props.onChangedMode(Number(event.target.id));
}}
>
{t.title}
</a>
</li>
);
}
return (
<nav>
<ol>{lis}</ol>
</nav>
);
}
function Article(props) {
return (
<article>
<h2>{props.title}</h2>
{props.body}
</article>
);
}
function App() {
const [mode, setMode] = useState("WELCOME");
const [id, setId] = useState(null);
const topics = [
{ id: 1, title: "html", body: "html is..." },
{ id: 2, title: "css", body: "cssl is..." },
{ id: 3, title: "javascript", body: "javascript is..." },
];
let content = null;
if (mode === "WELCOME") {
content = <Article title="Welcome" body="Hello, WEB"></Article>;
} else if (mode === "READ") {
let title,
body = null;
//topics id값에 따른 결과 출력
for (let i = 0; i < topics.length; i++) {
if (topics[i].id === id) {
title = topics[i].title;
body = topics[i].body;
}
}
content = <Article title={title} body={body}></Article>;
}
return (
<div className="App">
<Header
title="REACT"
onChangedMode={() => {
setMode("WELCOME");
}}
></Header>
<Nav
topics={topics}
onChangedMode={(_id) => {
setMode("READ");
setId(_id);
}}
></Nav>
{content}
</div>
);
}
export default App;
create
import "./App.css";
import { useState } from "react";
function Header(props) {
console.log("props", props, props.title);
return (
<header>
<h1>
<a href="/" onClick={(event) => {
event.preventDefault();
props.onChangedMode();
}}>
{props.title}
</a>
</h1>
</header>
);
}
function Nav(props) {
const lis = [];
for (let i = 0; i < props.topics.length; i++) {
let t = props.topics[i];
lis.push(
<li key={t.id}>
<a
id={t.id}
href={"/read/" + t.id}
onClick={(event) => {
event.preventDefault();
props.onChangedMode(Number(event.target.id));
}}
>
{t.title}
</a>
</li>
);
}
return (
<nav>
<ol>{lis}</ol>
</nav>
);
}
function Article(props) {
return (
<article>
<h2>{props.title}</h2>
{props.body}
</article>
);
}
function Create(props) {
return (
<article>
<h2>Create</h2>
<form
onSubmit={(event) => {
event.preventDefault();
const title = event.target.title.value;
const body = event.target.body.value;
props.onCreate(title, body);
}}
>
<p>
<input type="text" name="title" placeholder="title" />
</p>
<p>
<textarea name="body" placeholder="body"></textarea>
</p>
<p>
<input type="submit" value="Create" />
</p>
</form>
</article>
);
}
function App() {
const [mode, setMode] = useState("WELCOME");
const [id, setId] = useState(null);
const [nextId, setNextId] = useState(4);
const [topics, setTopics] = useState([
{ id: 1, title: "html", body: "html is..." },
{ id: 2, title: "css", body: "cssl is..." },
{ id: 3, title: "javascript", body: "javascript is..." },
]);
let content = null;
if (mode === "WELCOME") {
content = <Article title="Welcome" body="Hello, WEB"></Article>;
} else if (mode === "READ") {
let title,
body = null;
//topics id값에 따른 결과 출력
for (let i = 0; i < topics.length; i++) {
if (topics[i].id === id) {
title = topics[i].title;
body = topics[i].body;
}
}
content = <Article title={title} body={body}></Article>;
} else if (mode === "CREATE") {
content = (
<Create
onCreate={(_title, _body) => {
const newTopic = { id: nextId, title: _title, body: _body };
const newTopics = [...topics];
newTopics.push(newTopic);
setTopics(newTopics);
setMode("READ");
setId(nextId);
setNextId(nextId + 1);
}}
></Create>
);
}
return (
<div className="App">
<Header
title="REACT"
onChangedMode={() => {
setMode("WELCOME");
}}
></Header>
<Nav
topics={topics}
onChangedMode={(_id) => {
setMode("READ");
setId(_id);
}}
></Nav>
{content}
<a
href="/create"
onClick={(event) => {
event.preventDefault();
setMode("CREATE");
}}
>
Create
</a>
</div>
);
}
export default App;
update
import "./App.css";
import { useState } from "react";
function Header(props) {
console.log("props", props, props.title);
return (
<header>
<h1>
<a
href="/"
onClick={(event) => {
event.preventDefault();
props.onChangedMode();
}}
>
{props.title}
</a>
</h1>
</header>
);
}
function Nav(props) {
const lis = [];
for (let i = 0; i < props.topics.length; i++) {
let t = props.topics[i];
lis.push(
<li key={t.id}>
<a
id={t.id}
href={"/read/" + t.id}
onClick={(event) => {
event.preventDefault();
props.onChangedMode(Number(event.target.id));
}}
>
{t.title}
</a>
</li>
);
}
return (
<nav>
<ol>{lis}</ol>
</nav>
);
}
function Article(props) {
return (
<article>
<h2>{props.title}</h2>
{props.body}
</article>
);
}
function Create(props) {
return (
<article>
<h2>Create</h2>
<form
onSubmit={(event) => {
event.preventDefault();
const title = event.target.title.value;
const body = event.target.body.value;
props.onCreate(title, body);
}}
>
<p>
<input type="text" name="title" placeholder="title" />
</p>
<p>
<textarea name="body" placeholder="body"></textarea>
</p>
<p>
<input type="submit" value="Create" />
</p>
</form>
</article>
);
}
function Update(props) {
const [title, setTitle] = useState(props.title);
const [body, setBody] = useState(props.body);
return (
<article>
<h2>Update</h2>
<form
onSubmit={(event) => {
event.preventDefault();
const title = event.target.title.value;
const body = event.target.body.value;
props.onUpdate(title, body);
}}
>
<p>
<input
type="text"
name="title"
placeholder="title"
value={title}
onChange={(event) => {
setTitle(event.target.value);
}}
/>
</p>
<p>
<textarea
name="body"
placeholder="body"
value={body}
onChange={(event) => {
setBody(event.target.value);
}}
></textarea>
</p>
<p>
<input type="submit" value="Update" />
</p>
</form>
</article>
);
}
function App() {
const [mode, setMode] = useState("WELCOME");
const [id, setId] = useState(null);
const [nextId, setNextId] = useState(4);
const [topics, setTopics] = useState([
{ id: 1, title: "html", body: "html is..." },
{ id: 2, title: "css", body: "cssl is..." },
{ id: 3, title: "javascript", body: "javascript is..." },
]);
let content = null;
let contextControl = null;
if (mode === "WELCOME") {
content = <Article title="Welcome" body="Hello, WEB"></Article>;
} else if (mode === "READ") {
let title,
body = null;
//topics id값에 따른 결과 출력
for (let i = 0; i < topics.length; i++) {
if (topics[i].id === id) {
title = topics[i].title;
body = topics[i].body;
}
}
content = <Article title={title} body={body}></Article>;
contextControl = (
<li>
<a
href={"/update/" + id}
onClick={(event) => {
event.preventDefault();
setMode("UPDATE");
}}
>
Update
</a>
</li>
);
} else if (mode === "CREATE") {
content = (
<Create
onCreate={(_title, _body) => {
const newTopic = { id: nextId, title: _title, body: _body };
const newTopics = [...topics];
newTopics.push(newTopic);
setTopics(newTopics);
setMode("READ");
setId(nextId);
setNextId(nextId + 1);
}}
></Create>
);
} else if (mode === "UPDATE") {
let title,
body = null;
//topics id값에 따른 결과 출력
for (let i = 0; i < topics.length; i++) {
if (topics[i].id === id) {
title = topics[i].title;
body = topics[i].body;
}
}
content = (
<Update
title={title}
body={body}
onUpdate={(title, body) => {
console.log(title, body);
const newTopics = [...topics];
const updatedTopic = { id: id, title: title, body: body };
for (let i = 0; i < newTopics.length; i++) {
if (newTopics[i].id === id) {
newTopics[i] = updatedTopic;
break;
}
}
setTopics(newTopics);
setMode("READ");
}}
></Update>
);
}
return (
<div className="App">
<Header
title="REACT"
onChangedMode={() => {
setMode("WELCOME");
}}
></Header>
<Nav
topics={topics}
onChangedMode={(_id) => {
setMode("READ");
setId(_id);
}}
></Nav>
{content}
<ul>
<li>
<a
href="/create"
onClick={(event) => {
event.preventDefault();
setMode("CREATE");
}}
>
Create
</a>
</li>
{contextControl}
</ul>
</div>
);
}
export default App;
Author And Source
이 문제에 관하여(생활코딩 리액트), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@cgokk/생활코딩-리액트저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)