๐Ÿ“–[React] POST(์ƒ์„ฑ), useHistory()

ํ˜ฑยท2022๋…„ 10์›” 18์ผ

React

๋ชฉ๋ก ๋ณด๊ธฐ
27/28

โœ”๏ธ ํ•„์š”ํ•œ data ํ™•์ธ

data.json

 {
      "id": 2,
      "day": 1,
      "eng": "apple",
      "kor": "์‚ฌ๊ณผ",
      "isDone": true
    },

day, eng, kor๋Š” ์ž…๋ ฅ๋ฐ›๊ณ  isDone์„ false๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ์ €์žฅํ•  ์˜ˆ์ •

๐Ÿ“Œ POST ํ™œ์šฉ

CreateWord.js

import React from "react";
import { useRef } from "react";
import useFetch from "../hooks/useFetch";

export default function CreateWord() {
  const days = useFetch("http://localhost:3001/days");

  function onSubmit(e) {
    e.preventDefault();

    console.log(engRef.current.value);
    console.log(korRef.current.value);
    console.log(dayRef.current.value);

    fetch(`http://localhost:3001/words/`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        day: dayRef.current.value,
        eng: engRef.current.value,
        kor: korRef.current.value,
        isDone: false,
      }),
    }).then((res) => {
      if (res.ok) {
        alert("์ƒ์„ฑ์ด ์™„๋ฃŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค");
      }
    });
    
  }
  const engRef = useRef(null);
  const korRef = useRef(null);
  const dayRef = useRef(null);

  return (
    <form onSubmit={onSubmit}>
      <div className="input_area">
        <label>Eng</label>
        <input type="text" placeholder="computer" ref={engRef} />
      </div>
      <div className="input_area">
        <label>Kor</label>
        <input type="text" placeholder="์ปดํ“จํ„ฐ" ref={korRef} />
      </div>
      <div className="input_area">
        <label>Day</label>
        <select ref={dayRef}>
          {days.map((day) => (
            <option key={day.id} value={day.day}>
              {day.day}
            </option>
          ))}
        </select>
      </div>
      <button>์ €์žฅ</button>
    </form>
  );
}

์—ฌ๊ธฐ์„œ,

function onSubmit(e) {
    e.preventDefault();

    console.log(engRef.current.value);
    console.log(korRef.current.value);
    console.log(dayRef.current.value);

    fetch(`http://localhost:3001/words/`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        day: dayRef.current.value,
        eng: engRef.current.value,
        kor: korRef.current.value,
        isDone: false,
      }),
    }).then((res) => {
      if (res.ok) {
        alert("์ƒ์„ฑ์ด ์™„๋ฃŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค");
      }
    });
    
  }
  const engRef = useRef(null);
  const korRef = useRef(null);
  const dayRef = useRef(null);

์ „๋‹ฌํ•  body์— ๊ฐ ์ „๋‹ฌ ๊ฐ’์„ ๋„ฃ์–ด์คŒ

์ƒ์„ฑ ์™„๋ฃŒ ํ›„์— ํ•ด๋‹นํ•˜๋Š” ํŽ˜์ด์ง€๋กœ ์ด๋™

๐Ÿ“Œ useHistory()

useHistory()๋Š” react-router์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ
const history= useHistory();
history.push(์›ํ•˜๋Š” ์ด๋™ url);
useHistory()๋กœ ์„ค์ •ํ•œ history์— ์ฃผ์†Œ๋ฅผ pushํ•˜๋ฉด ํ•ด๋‹น ํŽ˜์ด์ง€๋กœ ์ด๋™

CreateWord.js

import React from "react";
import { useRef } from "react";
import { useHistory } from "react-router-dom";
import useFetch from "../hooks/useFetch";

export default function CreateWord() {
  const days = useFetch("http://localhost:3001/days");
  const history = useHistory();

  function onSubmit(e) {
    e.preventDefault();

    fetch(`http://localhost:3001/words/`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        day: dayRef.current.value,
        eng: engRef.current.value,
        kor: korRef.current.value,
        isDone: false,
      }),
    }).then((res) => {
      if (res.ok) {
        alert("์ƒ์„ฑ์ด ์™„๋ฃŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค");
        history.push(`/day/${dayRef.current.value}`);
      }
    });
  }
  const engRef = useRef(null);
  const korRef = useRef(null);
  const dayRef = useRef(null);

  return (
    <form onSubmit={onSubmit}>
      <div className="input_area">
        <label>Eng</label>
        <input type="text" placeholder="computer" ref={engRef} />
      </div>
      <div className="input_area">
        <label>Kor</label>
        <input type="text" placeholder="์ปดํ“จํ„ฐ" ref={korRef} />
      </div>
      <div className="input_area">
        <label>Day</label>
        <select ref={dayRef}>
          {days.map((day) => (
            <option key={day.id} value={day.day}>
              {day.day}
            </option>
          ))}
        </select>
      </div>
      <button>์ €์žฅ</button>
    </form>
  );
}

๐Ÿ“Œ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋‚ ์งœ ์ƒ์„ฑ page ์ƒ์„ฑ

App.js์™€ Header.js ์—์„œ router ๋ฐ ๋ฒ„ํŠผ ์—ฐ๊ฒฐ

CreateDay.js

import { useHistory } from "react-router-dom";
import useFetch from "../hooks/useFetch";

export default function CreateDay() {
  const days = useFetch("http://localhost:3001/days");
  const history = useHistory();

  function addDay(e) {
    e.preventDefault();

    fetch(`http://localhost:3001/days/`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        day: days.length + 1,
      }),
    }).then((res) => {
      if (res.ok) {
        alert("์ƒ์„ฑ์ด ์™„๋ฃŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค");
        history.push(`/`);
      }
    });
  }

  return (
    <div>
      <h3> ํ˜„์žฌ ์ผ์ˆ˜ : {days.length}์ผ</h3>
      <button onClick={addDay}> Day ์ถ”๊ฐ€ </button>
    </div>
  );
}
profile
new blog: https://hae0-02ni.tistory.com/

0๊ฐœ์˜ ๋Œ“๊ธ€