ํฐ์คํ ๋ฆฌ ๋ทฐ
[React Study] ์ํ์ฝ๋ฉ React - 17.1 ์ปดํฌ๋ํธ ์ด๋ฒคํธ ๋ง๋ค๊ธฐ ~ React - 22. ์์ ์ ๋ง์น๋ฉฐ
๊ตฌ๋์ 2022. 5. 13. 01:41
React - 17.1. ์ปดํฌ๋ํธ ์ด๋ฒคํธ ๋ง๋ค๊ธฐ
return (
<div className="App">
<Subject
title = {this.state.subject.title}
sub = {this.state.subject.sub}>
</Subject>
{</*header>
<h1><a href ="/" onClick={function(e){
console.log(e);
e.preventDefault();
this.setState({
mode:'welcome'
});
alert('hi');
}.bind(this)}>{this.state.subject.title}</a></h1>
{this.state.subject.sub}
</header>*/}
<TOC data = {this.state.contents}></TOC>
<Content title = "HTML" desc = {_desc}></Content>
</div>
);
- header ํ๊ทธ๋ฅผ ์ฃผ์์ฒ๋ฆฌํ๊ณ ์๋ ์ฐ๋ ํ๊ทธ ์ด๋ฆผ
- ์ด๋ฒคํธ ์ฒ๋ฆฌ๋ฅผ ํ๊ณ ์ถ๋ค๋ฉด onChangePage ์ด๋ฒคํธ๋ฅผ ์ธ ์ ์์
- a ํ๊ทธ๊ฐ ํด๋ฆญ๋์์ ๋ ์ค์นํ ํจ์๋ฅผ ์คํํด ์ค
- ์ฐ๋ฆฌ๊ฐ ๋ง๋๋ ค๊ณ ํ๋ Subject ์ปดํฌ๋ํธ๋ onChangePage ์ด๋ฒคํธ๊ฐ ์์ด์ ์ด ์ปดํฌ๋ํธ ์์์ ๋งํฌ๋ฅผ ํด๋ฆญํ์ ๋ ์ด๋ฒคํธ์ ์ค์นํ ํจ์๋ฅผ ๋ง๋๋ ค๊ณ ํจ
<div className="App">
<Subject
title = {this.state.subject.title}
sub = {this.state.subject.sub}
onChangePage={function(){
alert('hihihi');
}.bind(this)}
>
</Subject>
- App.js์ ์ถ๊ฐ
- onChangePage
<header>
<h1><a href ="/" onClick={function(e){
e.preventDefault();
this.props.onChangePage();
}.bind(this)}>{this.props.title}</a></h1>
{this.props.sub}
</header>
- Subject.js์ ์ถ๊ฐ
- ๋งํฌ ํ๊ทธ์ onClick ์์ฑ ์ถ๊ฐํด์ฃผ๊ณ preventDefault๋ก ํ์ด์ง ๋ฆฌ๋ก๋๋์ง ์๊ฒ ๋ง๋ฌ
- onChangePage ํจ์ ์คํ
- WEB์ ๋๋ ์ ๋ ์คํ์ด ์ ๋๋๊ฒ์ ํ์ธํ ์ ์์
return (
<div className="App">
<Subject
title = {this.state.subject.title}
sub = {this.state.subject.sub}
onChangePage={function(){
this.setState({mode:'welcome'});
}.bind(this)}
>
- alert ์ค์ ์ง์ฐ๊ณ mode change ์ถ๊ฐ
- WEB์ ๋๋ฅด๋ ๋ชจ๋๊ฐ ์ ๋ฐ๋๋ ๊ฒ์ ๋ณผ ์ ์์
React - 17.2. ์ปดํฌ๋ํธ ์ด๋ฒคํธ ๋ง๋ค๊ธฐ
๊ธ ๋ชฉ๋ก์ ํด๋ฆญํ์ ๋ App component์ state mode๋ฅผ read๋ก ๋ฐ๊พธ๊ณ ํด๋ฆญํ ๊ฒ์ ํด๋น๋๋ ์ปจํ ํธ๊ฐ ๋ณธ๋ฌธ์ ์ถ๋ ฅ๋๋๋ก ํจ
<TOC onChangePage={function(){
this.setState({mode:'read'});
}.bind(this)}
data = {this.state.contents}
></TOC>
- App.js TOC ์์
while(i < data.length){
// key ๊ฐ์ ์ง์ ํด ์ฃผ์ด์ผ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ง ์์
lists.push(<li key = {data[i].id}>
<a
href = {"/contents/" + data[i].id}
onClick={function(e){
e.preventDefault();
this.props.onChangePage();
}.bind(this)}
> {data[i].title} </a>
</li>);
i = i + 1;
}
- TOC.js ์์
React - 17.3. ์ปดํฌ๋ํธ ์ด๋ฒคํธ ๋ง๋ค๊ธฐ
selected_content_id_2,
- 2๋ฒ์ผ ๋ ์คํ
render() {
console.log('App render');
var _title, _desc = null;
if(this.state.mode === 'welcome'){
_title = this.state.welcome.title;
_desc = this.state.welcome.desc;
}
else if(this.state.mode === 'read'){
var i = 0;
while(i < this.state.contents.length){
var data = this.state.contents[i];
if(data.id === this.state.selected_content_id){
_title = data.title;
_desc = data.desc;
break;
}
i = i + 1;
}
}
- while๋ฌธ์ผ๋ก id๊ฐ 2์ผ ๋ title๊ณผ desc์ ๊ฐ์ ๋ฃ์ด์ค
- TOC์์ ํจ์๋ฅผ ํธ์ถํ ๋ ์ฐ๋ฆฌ๊ฐ ํด๋ฆญํ ํญ๋ชฉ์ id ๊ฐ์ ์ฃผ๋ฉด ๋์ง ์์๊น?
<TOC onChangePage={function(id){
this.setState({
mode:'read',
selected_content_id:Number(id)
});
}.bind(this)}
data = {this.state.contents}
></TOC>
- App.js ์์ TOC ๋ณ๊ฒฝ
<a
href = {"/contents/" + data[i].id}
data-id={data[i].id}
onClick={function(e){
e.preventDefault();
this.props.onChangePage(e.target.dataset.id);
}.bind(this)}
> {data[i].title} </a>
- TOC.js์์ onChangePage ๊ฐ ๋ณ๊ฒฝ
- HTML, CSS, JavaScript๋ฅผ ๋๋ฅผ ๋ ๊ฐ์ด ๋ฐ๋๋ ๊ฒ์ ์ ์ ์์
React - 18. ๋ฒ ์ด์ค ์บ ํ
state -> ์์ ํ ๋ set.state๋ก ์์ ๊ฐ๋ฅ
- props : read-only, cannot be modified ์์ ๋ถ๊ฐ๋ฅ
state์ props๋ฅผ ์ ์ ํ ์ฌ์ฉํด์ผ ํจ
- ์ฌ์ฅ๊ณผ ์ง์ ๊ฐ๋ ์ผ๋ก ๋น์ ํ๋ฉด ์ฌ์ฅ -> state, ์ง์ -> props
- ์ฌ์ฅ์ด ์ผ ๋ชฉ๋ก์ ๋ง๋ค๋ฉด ์ง์์ ๋ชฉ๋ก์ ์๋ ๊ฒ์ ๊ณจ๋ผ์ ์ผ์ ์ฒ๋ฆฌํ ์ ์์ง๋ง ์ง์์ด ์๋ก์ด ์ผ์ ํ๋ ค๋ฉด ์ด๋ฒคํธ๋ฅผ ๋ง๋ค์ด ์ฌ์ฅ์๊ฒ ํ๋ฝ์ ๊ตฌํด์ผ ํจ
React - 19. create ๊ตฌํ : ์๊ฐ
CRUD : Create Read Update Delete
React - 19.2 create ๊ตฌํ : mode ๋ณ๊ฒฝ ๊ฐ๋ฅ
Control.js
import { Component } from 'react';
class Control extends Component {
render(){
console.log('Subject render')
return (
<ul>
<li><a href="/create" onClick={function(e){
e.preventDefault();
this.props.onChangeMode('create');
}.bind(this)}>create</a></li>
<li><a href="/update" onClick={function(e){
e.preventDefault();
this.props.onChangeMode('update');
}.bind(this)}>update</a></li>
<li><input type="button" value="delete" onClick={function(e){
e.preventDefault();
this.props.onChangeMode('delete');
}.bind(this)}></input></li>
</ul>
);
}
}
export default Control;
- Subject.js๋ฅผ ๋ณต์ฌํด Control.js ๋ง๋ค๊ธฐ
- create, update, delete ๋ชจ๋ ๋ง๋ค๊ธฐ
<Control onChangeMode = {function(_mode){
this.setState({
mode:_mode
})
}.bind(this)}></Control>
- App.js์ ์ถ๊ฐ
- ์์ฑ ๋๋ฅด๋ฉด ๋ชจ๋๊ฐ ๋ณ๊ฒฝ๋๋๋ก ํจ
- ๋ชจ๋๊ฐ ์ ๋ณ๊ฒฝ๋๋ ๊ฒ ํ์ธ
React - 19.3. create ๊ตฌํ : mode ์ ํ ๊ธฐ๋ฅ
import { Component } from 'react';
class ReadContent extends Component{
render(){
console.log('Content render');
return(
<article>
<h2>{this.props.title}</h2>
{this.props.desc}
</article>
)
}
}
export default ReadContent;
- ReadContent ์ ์ํ๊ธฐ
- Content๋ฅผ ์์ ํ๊ธฐ
import { Component } from 'react';
class CreateContent extends Component{
render(){
console.log('Content render');
return(
<article>
<h2>Create</h2>
<form>
</form>
</article>
)
}
}
export default CreateContent;
- CreateContent ์ ์
var _title, _desc, _article = null;
if(this.state.mode === 'welcome'){
_title = this.state.welcome.title;
_desc = this.state.welcome.desc;
_article = <ReadContent title={_title} desc={_desc}></ReadContent>
}
- article์ ์ถ๊ฐํ๊ณ , Content๊ฐ ์์๋ ๊ณณ์ {_article}๋ก ์์
- ReadContent๊ฐ ์ ๋ณด์ด๋ ๊ฒ ํ์ธ
_article = <ReadContent title={_title} desc={_desc}></ReadContent>
} else if(this.state.mode === 'create'){
_article = <CreateContent></CreateContent>
}
- App.js์ ์ฝ๋ ์ถ๊ฐ
- App mode๊ฐ create์ผ ๋ CreateContent๊ฐ ๋ณด์
React - 19.4. create ๊ตฌํ: form
<form>
<p><input type="text" name="title" placeholder="title"></input></p>
</form>
- CreateContent.js์์ form ์์ ์์ฑ
- placeholder : ํผ ์์ ๊ฐ์ด ์๋ฌด๊ฒ๋ ์์ ๋ 'title' ์ถ๋ ฅํ๋ ๊ฒ
mode: 'create',
- App.js์ mode์ ๊ธฐ๋ณธ๊ฐ์ create๋ก ์ค์
<p><input type="text" name="title" placeholder="title"></input></p>
<p>
<textarea name="desc" placeholder="descripttion"></textarea>
</p>
<p>
<input type="submit"></input>
- CreateContent ๋ด์ฉ ์ถ๊ฐ
<form action="/create_process" method="post" onSubmit={function(e){
e.preventDefault();
alert('Submit !!');
}.bind(this)}>
- ํ์ด์ง ๋ก๋ฉ์ด ๋์ง ์๊ฒ ํจ
React - 19.5. create ๊ตฌํ: onSubmit ์ด๋ฒคํธ
<form action="/create_process" method="post" onSubmit={function(e){
e.preventDefault();
this.props.onSubmit(
e.target.title.value,
e.target.desc.value
);
- CreateContent.js์์ e.target.title.value์ e.target.desc.value ์ถ๊ฐ
- ํผ์ ๋ด์ฉ์ ์ ๋ ฅํ์ ๋ ์ฝ์์์ ์ ๋ ฅํ ๊ฐ์ด ๋ณด์ด๋ ๊ฒ์ ์ ์ ์์
React - 19.6. create ๊ตฌํ: contents ๋ณ๊ฒฝ
this.max_content_id = 3;
- App.js์ ์ถ๊ฐ
this.max_content_id = this.max_content_id + 1;
this.state.contents.push(
{id:this.max_content_id, title:_title, desc:_desc}
);
this.setState({
contents:this.state_contents
});
- ๋ฆฌ์กํธ์ ๋์ค์ ์ฑ๋ฅ์ ์ถ๊ฐํ ๋ ํ๋ค ์ ์๋ ์ฝ๋
- concat : ์๋ณธ์ ๋ณ๊ฒฝํ ์๋ก์ด ๋ฐฐ์ด์ด ๋ฆฌํด๋จ
- push์ ๊ฐ์ ์ค๋ฆฌ์ง๋ ๋ฐ์ดํฐ๋ฅผ ์์ ํ๋ ๊ฒ์ ์ฐ์ง ๋ง๊ณ concat์ ์ธ ๊ฒ
var _contents = this.state.contents.concat(
{id:this.max_content_id, title:_title, desc:_desc}
)
- ์ฑ๋ฅ ๊ฐ์ ์ ์ํด ์ข์ ๋ฐฉ๋ฒ
React - 19.8. create ๊ตฌํ: immutable
immutable : ์๋ณธ์ด ๋ถ๋ณํ๋ค
React - 20.1. update ๊ตฌํ
import { Component } from 'react';
class UpdateContent extends Component{
render(){
console.log('UpdateContent render');
return(
<article>
<h2>Update</h2>
<form action="/create_process" method="post" onSubmit={function(e){
e.preventDefault();
this.props.onSubmit(
e.target.title.value,
e.target.desc.value
);
}.bind(this)}>
<p><input type="text" name="title" placeholder="title"></input></p>
<p>
<textarea name="desc" placeholder="descripttion"></textarea>
</p>
<p>
<input type="submit"></input>
</p>
</form>
</article>
)
}
}
export default UpdateContent;
- UpdateContent.js ์ ์
} else if(this.state.mode === 'update'){
_article = <UpdateContent onSubmit={function(_title, _desc){
this.max_content_id = this.max_content_id + 1;
var _contents = this.state.contents.concat(
{id:this.max_content_id, title:_title, desc:_desc}
)
this.setState({
contents:_contents
});
console.log(_title, _desc);
}.bind(this)}></UpdateContent>
}
console.log('render', this);
- App.js๋ก ์ถ๊ฐ
- update๋ฅผ ๋๋ ์ ๋ ์ ์ ํ
React - 20.2. update ๊ตฌํ : form
props์ ๋ฐ์ดํฐ๋ read only๋๊น ์์ ํ์ง ๋ชปํจ
UpdateContent.js
value={this.props.data.title}
- ์์ ๋ถ๊ฐ
constructor(props){
super(props);
this.state = {
title:this.props.data.title
}
}
- ์ถ๊ฐํด๋ ์์ ์ด ๋์ง ์์
- input์ ๊ฐ์ ๋ฐ๊ฟจ์ ๋ state์ ๊ฐ๋ ๋ฐ๋๊ฒ ํด์ผ ํจ
inputFormHandler(e){
this.setState({[e.target.name]:e.target.value});
}
- ํ์ฌ target์ ์ด๋ฆ์ ์๊ฒ ํด์ฃผ๋ ๊ฒ
this.inputFormHandler = this.inputFormHandler.bind(this);
- binding์ด ๋ ๊ฒ์ผ๋ก this๋ฅผ ๋ฐ๊ฟ์ค
onChange={this.inputFormHandler}
- ์ฝ๋๋ฅผ ๋ณด๋ค ๋ ๊น๋ํ๊ฒ ๋ง๋ค์ด ์ค
React - 20.3. update ๊ตฌํ : state ๋ณ๊ฒฝ
<input type="hidden" name="id" value={this.state.id}></input>
- ์ฌ์ฉ์ ๋์ ๋ณด์ด์ง ์๊ฒ ํ๋ ๊ฒ
Array.from(this.state.contents);
- ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ๋ณต์ ํ ์๋ก์ด Array ๋ง๋ค์ด์ง
function(_id, _title, _desc){
var _contents = Array.from(this.state.contents);
var i = 0;
while(i < _contents.length){
if(_contents[i].id === _id){
_contents[i] = {id:_id, title:_title, desc:_desc}
break;
}
i = i + 1;
}
- App.js ์์
- ์ ๋ณ๊ฒฝ๋๋ ๊ฒ ํ์ธ
this.setState({
contents:_contents,
mode: 'read'
- ๊ฐ์ create, update๊ฐ ์คํ๋๊ณ ๋์ mode๋ฅผ read๋ก ๋ณ๊ฒฝํด์ฃผ๋ ์ฝ๋ ์ถ๊ฐ
- create ์ดํ ์ถ๊ฐํ ์คํฌ๋ฆฝํธ๊ฐ ๋์ ๋ฐ๋ก ๋ณด์ด๋ ๊ฒ์ ์ ์ ์์
React - 21.delete ๊ตฌํ
<Control onChangeMode = {function(_mode){
if(_mode === 'delete'){
if(window.confirm('really?')){
var _contents = Array.from(this.state.contents);
var i = 0;
while(i < this.state.contents.length){
if(_contents[i].id === this.state.selected_content_id){
_contents.splice(i, 1);
break;
}
i = i + 1;
}
this.setState({
mode:'welcome',
contents:_contents
})
}
} else{
this.setState({
mode:_mode
})
}
}.bind(this)}></Control>
- App.js์ ์ญ์ ๊ธฐ๋ฅ ์ถ๊ฐ
- delete ๋ฒํผ์ ๋๋ฅด๋ฉด really? ๋ผ๋ ์ฐฝ์ด ๋จ๊ณ
- ์ญ์ ๋์๋ค๋ ๋ฌธ๊ตฌ
- ์ ์ญ์ ๋์๊ณ welcome์ผ๋ก ๋์๊ฐ ๊ฒ์ ๋ณผ ์ ์์
'๐ STUDY > ๐ React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
- ํ์ผ ์ ์ถ๋ ฅ
- MySQL
- Programming
- react-scripts
- ์๊ณ ๋ฆฌ์ฆ
- C
- ๋ฐฑ์ค
- bitnami
- ์ค์น
- react
- GIT
- ๋จธ์ ๋ฌ๋
- machine learning
- ๋ค์คํ๋ก์ธ์ค
- C++
- Annotation
- ํ๋ก๊ทธ๋๋ฐ
- ๋ฒํผ
- error
- ํ๋ก์ธ์ค
- ์ค๋ฅ
- PHP
- 9086๋ฒ
- Baekjoon
- Linux
- ๋ฆฌ๋ ์ค
- SpringBoot
- ๊ธฐ๊ณํ์ต
- Apache
- SWiFT
- Total
- Today
- Yesterday