Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- customModal component 만들기
- react-query
- google firebase
- 배열
- useMutation
- Next
- 파이어베이스
- React
- 달리기경주
- Recoil
- programmers
- 리엑트
- JavaScript
- 리액트
- 넥스트
- todolist
- 생명주기
- Firebase
- debounce
- JS
- 겹치는 선분의 길이
- Redux
- SSR
- next-pwa
- csr
- lifecycle
- 투두리스트
- 자바스크립트
- 리덕스
- NextJS
Archives
- Today
- Total
끄적끄적
[React+Redux]간단하게 만들어보는 Todolist(1) 본문
오늘은 Redux를 이용한 아주 간단한 TodoList를 만들어보려고합니다.
! Redux가 무엇인지 잘 모르시는 분은 pyolog.tistory.com/7 을 참고하시면 좋을 것 같습니다.
1. 우선 cra(create-react-app)로 리액트 프로젝트를 생성하겠습니다.
npx crate-react-app redux_test
2. 먼저 src 폴더 밑에 store를 작성하겠습니다.
import { createStore } from "redux";
// 초기 state , 배열에 저장하겠습니다.
export const INITAIL_STATE = {todos : []};
//■■■■■■■■■■■■■■■■■■■■■■■ action 정의 ■■■■■■■■■■■■■■■■■■■■■■■■■■■
export function AddTodo(content){
return{
type: 'ADD',
content
}
}
export function DeleteTodo(id){
return{
type : 'DELETE',
id
}
}
export function UpdateTodo(id, content){
return{
type : 'UPDATE',
id,
content
}
}
//■■■■■■■■■■■■■■■■■■■■■■■ reducer 정의 ■■■■■■■■■■■■■■■■■■■■■■■■■■■
export const Reducer = (state = INITAIL_STATE, action)=> {
switch(action.type){
case 'ADD' :
return {
...state,
todos : [...state.todos, action.content]
};
case 'DELETE' :
return{
...state,
todos : state.todos.filter((data,index) => index !== action.id)
}
case 'UPDATE' :
return{
...state,
todos : state.todos.map((data,index)=>{
if(index === action.id){
return action.content;
}else return data;
})
}
default :
return state;
}
}
export const store = createStore(Reducer);
export default store;
저희가 투두리스트에서 사용할 기능들은 생성, 수정, 삭제 크게 3가지 입니다.
3. index.js에 store연결하기
// 연결시켜주기위해 필요한 파일들을 가져옵니다
import store from './store';
import { Provider } from 'react-redux';
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>
, document.getElementById("root")
);
이제 어느정도 연결이 완료되었습니다.
다음 글에서 컴포넌트들의 작성을 해보도록 하겠습니다.
'React' 카테고리의 다른 글
[React+네이버지도]React + NaverMaps 사용하여 지도 띄우기 (1) | 2020.12.07 |
---|---|
[React+Redux]간단하게 만들어보는 TodoList(2) (2) | 2020.11.27 |
[상태관리라이브러리] Redux(리덕스) (0) | 2020.11.25 |
[React] 자주 쓰이는 React 생명 주기(함수형편, useEffect) (0) | 2020.11.25 |
[React] 자주 쓰이는 React 생명 주기(Class편) (0) | 2020.11.25 |
Comments