const App(){
return ();
}
state, 라이플사이클 API 사용 불가능 -> Hooks 기능으로 해결
선언하기 용이, 메모리 자원 효율성, 결과물의 파일 크기 감소
단축기 rsc
function App(){
return ();
}
import React from 'react';
const App = () => {
return ();
}
export default App;
import MyComponent from './MyComponent'
const App = () =>{
return <MyComponent name="React">
}
const MyComponent = (props) =>{
return <div>안녕하세요 제 이름은 {props.name}입니다.</div>
}
const MyComponent = (props) => {
...
}
MyComponent.defaultProps = {
name : '기본 이름'
}
<부모 컴포넌트>
const App = () =>{
// 태그 사이의 값 '리액트'
return <MyComponent>리액트</MyComponent>
}
<자식 컴포넌트>
const MyComponent = (props) =>{
return <div>children 값은 {props.children}</div>
}
<부모 컴포넌트>
const {name, children} = props;
props.name 대신 name
props.children 대신 children
<자식 컴포넌트>
const MyComponent({name, children})
MyComponent.propTypes ={
name: PropTypes.string
}
MyComponent.propTypes ={
favoritNumber : PropTypes.number.isRequired
}
render(){
const {name, favoritNumber, children} = this.props;
}
import {Component} from 'react';
constructor(props){
super(props);
this.state ={
number: 0,
fixedNumber: 0
};
}
<button onClick = {()=> {
this.setState({number: number +1 })
}}
state = {
number: 0,
fixedNumber: 0
}
prevState
: 기존상태 props
: 현재 지니고 있는 props. 안쓴다면 생략가능<button
this.setState((prevState, (props))=>{
return {
number: prevState.number + 1;
}
})
></button>
this.setState({
number:number+1
}, ()=>{
// 콜백함수
}
)
const array = [1,2];
const one = array[0]
const two = array[1]
// 아래 코드와 동일하다.
const array = [1,2];
const [one, two] = array;
import {useStata} from 'react';
const Say = () => {
const [message, setMessage] = useState('');
const onClickEnter = () => setMessage('안녕하세요!')
const onClickLeave = () => setMessage('안녕히 가세요!')
}
const array = [
{id : 1, value : true},
{id : 2, value : false},
{id : 3, value : true},
]
let nextArray = array.concat({id:4}) // id가 4인 객체 추가
nextArray.filter(item => item.id !== 2) // id가 2인 객체 제거
nextArray.map(item => item.id === 1 ? {...item, value:false})
// id가 1인 객체 값을 false로 변경