zoaseo
To Infinity And Beyond
zoaseo
전체 방문자
오늘
어제
  • 분류 전체보기 (763)
    • 개발이 좋아서 (381)
      • SAP가 좋아서 (3)
      • Java가 좋아서 (42)
      • Spring이 좋아서 (50)
      • JPA가 좋아서 (0)
      • QueryDSL이 좋아서 (26)
      • Docker가 좋아서 (7)
      • Redis가 좋아서 (7)
      • AWS가 좋아서 (5)
      • CI/CD가 좋아서 (6)
      • Troubleshooting이 좋아서 (4)
      • Kotlin이 좋아서 (7)
      • SQL이 좋아서 (6)
      • HTTP가 좋아서 (21)
      • JavaScript가 좋아서 (30)
      • TypeScript가 좋아서 (6)
      • Vue가 좋아서 (21)
      • Flutter가 좋아서 (61)
      • React가 좋아서 (20)
      • Redux(React)가 좋아서 (2)
      • Angular가 좋아서 (22)
      • HTML이 좋아서 (9)
      • CSS가 좋아서 (15)
      • PHP가 좋아서 (9)
      • Illustrator가 좋아서 (2)
    • 노력이 좋아서 (169)
    • 결과물이 좋아서 (14)
    • 코딩연습이 좋아서 (168)
      • 이론이 좋아서 (62)
      • SQL이 좋아서 (90)
    • 유용한 사이트가 좋아서 (28)
    • Github (2)

인기 글

티스토리

hELLO · Designed By 정상우.
zoaseo
노력이 좋아서

<step67>'react_useReducer'

<step67>'react_useReducer'
노력이 좋아서

<step67>'react_useReducer'

2022. 6. 24. 10:55

1) 

Counter2.js

// import React, { useState } from 'react';
import React, { useReducer } from 'react';
function reducer(state, action){
    switch(action.type){
        case "INCREASE":
            return state+1;
        case "DECREASE":
            return state-1;
        default:
            return state;
    }
}
const Counter2 = () => {
    // const [ number, setNumber ] = useState(0);
    
    // function Increase(){
    //     setNumber(number+1);
    // }
    // function Decrease(){
    //     setNumber(number-1);
    // }

    const [ number, dispatch ] = useReducer(reducer,0);

    function Increase(){
        dispatch({type:"INCREASE"});
    }
    function Decrease(){
        dispatch({type:"DECREASE"});
    }

    return (
        <div>
            <h2>{number}</h2>
            <button onClick={Decrease}>-1</button>
            <button onClick={Increase}>+1</button>
        </div>
    );
};

export default Counter2;

2) 

App.js

import './App.css';
import UserList from './components/UserList';
import { useState, useRef, useReducer } from 'react';
import CreateUser from './components/CreateUser';

const initialState = {
  inputs: {
    username: '',
    email: '',
  },
  users: [
    {
      id: 1,
      username: 'green',
      email: 'green@gmail.com',
      active: false,
    },
    {
      id: 2,
      username: 'blue',
      email: 'blue@gmail.com',
      active: false,
    },
    {
      id: 3,
      username: 'yellow',
      email: 'yellow@gmail.com',
      active: false,
    }
  ]
}
function reducer(state, action){
  switch(action.type){
    case "CHANGE_INPUT":
      return {
          ...state,
          inputs: {
              ...state.inputs,
              [action.name]: action.value,
          }
      };
    case "CREATE_USER":
      return {
          inputs: initialState.inputs,
          users: state.users.concat(action.user),
      };
    case "DELETE_USER":
      return {
          ...state,
          users: state.users.filter(user=> user.id !== action.id),
      };
    case "ACTIVE_USER":
      return {
          ...state,
          users: state.users.map(user=> 
            user.id === action.id ? {...user, active: !user.active } : user),
      };
    default:
      return state;
  }
}
function App() {
  //useReducer로 상태관리
  const [ state, dispatch ] = useReducer(reducer,initialState);
  const { users } = state;
  const { username, email } = state.inputs;
  const onChange = (e) => {
    const { name, value } = e.target;
    dispatch({
        type: "CHANGE_INPUT",
        name: name,
        value: value,
    })
  }
  const onCreate = () => {
    dispatch({
        type: "CREATE_USER",
        user: {
            id: nextId.current,
            username: username,
            email: email,
        }
    })
    nextId.current += 1;
  }
  const nextId = useRef(4);
  const onDelete = (id) => {
      dispatch({
          type: "DELETE_USER",
          id: id,
      })
  }
  const onToggle = (id) => {
      dispatch({
          type: "ACTIVE_USER",
          id: id,
      })
  }
  
  return (
    <div className="App">
        <CreateUser email={email} username={username} onChange={onChange} 
        onCreate={onCreate}></CreateUser>
        <UserList users={users} onDelete={onDelete} onToggle={onToggle} />
    </div>
  );
}

export default App;

3) 

App.js

import './App.css';
import { useState, useRef, useReducer } from 'react';
import TodoLists from './components/TodoLists';
import CreateTodo from './components/CreateTodo';

const initialState = {
    list: "",
    todos: [
        {
            id:1,
            list: "리액트 공부하기",
            isDone: false,
        },
        {
            id:2,
            list: "타입스크립트 공부하기",
            isDone: false,
        },
        {
            id:3,
            list: "리덕스 공부하기",
            isDone: false,
        }
    ]
}
function reducer(state, action){
    switch(action.type){
        case "CHANGE_INPUT":
            return {
                ...state,
                list: action.list,
            };
        case "CREATE_TODO":
            return {
                list: "",
                todos: state.todos.concat(action.todo)
            };
        case "DELETE_TODO":
            return {
                ...state,
                todos: state.todos.filter(todo=> todo.id !== action.id)
            };
        case "ISDONE_TODO":
            return {
                ...state,
                todos: state.todos.map(todo=>
                    todo.id === action.id ? {...todo, isDone: !todo.isDone} : todo)
            };
        default:
            return state;
    }
}
function App() {
    const [ state, dispatch ] = useReducer(reducer, initialState);
    const { list, todos } = state;
    
  
  const onChange = (e) => {
    dispatch({
        type: "CHANGE_INPUT",
        list: e.target.value,
    })
    console.log(list);
  }
  const nexId = useRef(4);
  const onCreate = () => {
    dispatch({
        type: "CREATE_TODO",
        todo: {
            id: nexId.current,
            list: list,
            isDone: false,
        }
    })
  }
  const onDelete = (id) => {
    dispatch({
        type: "DELETE_TODO",
        id: id,
    })
  }
  const onToggle = (id) => {
    dispatch({
        type: "ISDONE_TODO",
        id: id,
    })
  }


  return (
    <div className="App">
      <CreateTodo list={list} onChange={onChange} onCreate={onCreate}/>
      <TodoLists todos={todos} onDelete={onDelete} onToggle={onToggle}/>
    </div>
  );
}

export default App;

CreateTodo.js

import React from 'react';

const CreateTodo = ({onChange, list, onCreate}) => {
    return (
        <div>
            <h2>to do list</h2>
            <div>
                <input type='text' value={list} name='newlist' onChange={onChange}/>
                <button onClick={onCreate}>+</button>
            </div>
        </div>
    );
};

export default CreateTodo;

TodoList.js

import React from 'react';
import './TodoList.css';

const TodoList = ({todo, onDelete, onToggle}) => {
    return (
        <div>
            <span className={todo.isDone ? 'isDone' : ""} onClick={()=>{ onToggle(todo.id) }}>
                {todo.list}
            </span>
            <button onClick={()=>{ onDelete(todo.id) }}>X</button>
        </div>
    );
};

export default TodoList;

 

TodoLists.js

import React from 'react';
import TodoList from './TodoList';

const TodoLists = ({todos, onDelete, onToggle}) => {
    return (
        <div>
            {todos.map(todo=><TodoList todo={todo} key={todo.id} onDelete={onDelete} onToggle={onToggle}/>)}
        </div>
    );
};

export default TodoLists;

TodoList.css

.isDone {
    background-color: #eee;
}

'노력이 좋아서' 카테고리의 다른 글

<step68>'react_mashup-todolist(useReducer + context API)'  (0) 2022.06.27
<step68>'react_createContext'  (0) 2022.06.27
<step66>'react_styledcomponents'  (0) 2022.06.23
<step66>'react_scss'  (0) 2022.06.23
<step65>'react_useEffect'  (0) 2022.06.22
    To Infinity And Beyondzoaseo 님의 블로그입니다.

    티스토리툴바

    개인정보

    • 티스토리 홈
    • 포럼
    • 로그인

    단축키

    내 블로그

    내 블로그 - 관리자 홈 전환
    Q
    Q
    새 글 쓰기
    W
    W

    블로그 게시글

    글 수정 (권한 있는 경우)
    E
    E
    댓글 영역으로 이동
    C
    C

    모든 영역

    이 페이지의 URL 복사
    S
    S
    맨 위로 이동
    T
    T
    티스토리 홈 이동
    H
    H
    단축키 안내
    Shift + /
    ⇧ + /

    * 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.