[React] 상태관리(useState / useReducer) 복습

2022. 7. 4. 09:23·Stack/React

상태가 변경될 때 리랜더링 됨 (다시 그려짐)

1. useState(초기값) => [ state, f ]

* 상태값 관리

const newArr = useState(초기값)

const state = newArr[0]

const setState = newArr[1]

const [ state, setState ] = useState(초기값)

setState(변경된값)

 

2. useReducer()

* 어떤 경우에 상태가 어떻게 업데이트 될건지 관리

useReducer(함수, 초기값) => [ state, 함수를 실행시키는 함수 ]

const [ state, dispatch ] = useReducer(reducer, initialstate)

dispatch({액션})

dispatch({

                type: "DELETE_USER",

                id: 10

})

const users = [

                {

                                id:1,

                                name: "a",

                                age: 30,

                                active: false               

                },

                {},

                {}               

]

function reducer(state, action) {

                switch(action.type){

                                case 'USER_DELETE':

                                return state.filter(list=> list.id !== action.id);

                                case 'USER_CREATE':

                                return state;

                                case 'USER_TOGGLE':

                                return state;

                                default:

                                return state;

                }

}

'Stack > React' 카테고리의 다른 글

[React] Lamp 쇼핑몰 구현하기 5 (이미지 업로드 / 상품등록 기능 구현)  (0) 2022.07.04
[React] 파일 업로드 관리하기  (0) 2022.07.04
[React] npm start 오류(throw err;) 해결하기  (0) 2022.07.01
[React] Lamp 쇼핑몰 구현하기 4 (Sqlite 연동)  (0) 2022.07.01
[React] Lamp 쇼핑몰 구현하기 3 (서버에 연결하기)  (0) 2022.07.01
'Stack/React' 카테고리의 다른 글
  • [React] Lamp 쇼핑몰 구현하기 5 (이미지 업로드 / 상품등록 기능 구현)
  • [React] 파일 업로드 관리하기
  • [React] npm start 오류(throw err;) 해결하기
  • [React] Lamp 쇼핑몰 구현하기 4 (Sqlite 연동)
7ingout
7ingout
  • 7ingout
    Hello, 7ingout world!
    7ingout
  • 전체
    오늘
    어제
    • 분류 전체보기 (205)
      • Project (5)
      • Stack (173)
        • React (40)
        • JavaScript (50)
        • TypeScript (14)
        • HTML (11)
        • CSS (31)
        • Spring (9)
        • PHP (15)
        • SQL (3)
        • Python (0)
      • ETC (9)
      • Design (13)
        • Illustrator (6)
        • Photoshop (7)
      • Articloid (4)
        • 7ingout (4)
  • 공지사항

    • ☻
  • 인기 글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
7ingout
[React] 상태관리(useState / useReducer) 복습
상단으로

티스토리툴바