배열 안에 들어있는 값을 쉽게 추출할 수 있도록 해주는 문법
array 안에 있는 값을 변수 one과 two에 저장
const array = [1, 2];
const one = array[0];
const two = array[1];
배열 비구조화 할당을 사용하여 저장
const array = [1, 2];
const [one, two] = array;
const [value, setValue] = useState(값);
// 인사의 초기값: 안녕하세요
const [인사, 인사변경] = useState('안녕하세요');
<button onClick={클릭할때_실행할_함수}>클릭</button>
function 클릭할때_실행할_함수(){
// 실행할 내용
}
const [value, setValue] = useState('안녕하세요');
value = '반가워요';
setValue('반가워요');
const [value, setValue] = useState({a:1, b:1});
value.b = 2;
객체/배열의 기존 내용을 변경하지 않고도 새로운 객체/배열을 생성할 수 있음
예) 객체 사본 만들기
const object = { a : 1, b : 2, c : 3 };
// object 객체의 사본 생성 후, b값만 변경
const copyObject = { …object, b : 50 };
console.log(copyObject); // { a : 1, b : 50, c : 3 }
import React from 'react';
function Spread(){
const person1 = { name : 'sooa' };
const person2 = { name : 'sooa', age : 20 };
const person3 = { name : 'sooa', age : 20, region : 'seoul' };
return (
<div>
<h1>{JSON.stringify(person1)}</h1>
<h1>{JSON.stringify(person2)}</h1>
<h1>{JSON.stringify(person3)}</h1>
</div>
);
}
export default Spread;

import React from 'react';
function Spread(){
const person1 = { name : 'sooa' };
const person2 = person1;
person2.age = 20;
const person3 = person2;
person3.region = 'seoul';
~~~~
return (
<div>
<h1>{JSON.stringify(person1)}</h1>
<h1>{JSON.stringify(person2)}</h1>
<h1>{JSON.stringify(person3)}</h1>
</div>
);
}
export default Spread;

import React from 'react';
function Spread(){
const person1 = { name : 'sooa' };
const person2 = { ...person1, age : 20 };
const person3 = { ...person2, region : 'seoul' };
return (
<div>
<h1>{JSON.stringify(person1)}</h1>
<h1>{JSON.stringify(person2)}</h1>
<h1>{JSON.stringify(person3)}</h1>
</div>
);
}
export default Spread;

import React from 'react';
function Spread(){
const person1 = { name : 'sooa' };
const person2 = { ...person1, age : 20 };
const person3 = { ...person2, region : 'seoul' };
const person4 = { ...person3, region : 'busan' };
return (
<div>
<h1>{JSON.stringify(person1)}</h1>
<h1>{JSON.stringify(person2)}</h1>
<h1>{JSON.stringify(person3)}</h1>
<h1>{JSON.stringify(person4)}</h1>
</div>
);
}
export default Spread;
import React from 'react';
function Spread(){
const person1 = { name : 'sooa'};
const person2 = { ...person1, age : 20};
const person3 = { ...person2, region : 'seoul'};
const person4 = { region : 'busan', ...person3 };
return (
<div>
<h1>{JSON.stringify(person1)}</h1>
<h1>{JSON.stringify(person2)}</h1>
<h1>{JSON.stringify(person3)}</h1>
<h1>{JSON.stringify(person4)}</h1>
</div>
);
}
export default Spread;
import React, { Component } from 'react';
import ClassState from './ClassState';
class App extends Component {
render() {
return (
<div>
<ClassState />
</div>
);
}
}
export default App;
import React, { Component } from 'react';
class ClassState extends Component {
constructor(props){
super(props);
this.state = {
인사 : '안녕하세요'
};
}
render() {
return (
<div>
<h1>{this.state.인사}</h1>
</div>
);
}
}
export default ClassState;
// 생략
class ClassState extends Component {
// 생략
changeMsg = () => {
console.log(“잘 나오나요?”);
}
render() {
return (
<div>
<h1>{this.state.인사}</h1>
<button onClick={this.changeMsg}>변경</button>
</div>
);
}
}
// 생략
// 생략
class ClassState extends Component {
state = {
인사 : '안녕하세요'
};
changeMsg = () => {
this.setState({ 인사 : 'Hello' });
}
render() {
return (
<div>
<h1>{this.state.인사}</h1>
<button onClick={this.changeMsg}>변경</button>
</div>
);
}
}
// 생략
💡 값 변경하는 법
