on
React.js의 공리와 함께 작업
React.js의 공리와 함께 작업
반응형
이 기사에 내가 쓸 내용은
먼저 프로젝트 디렉터리에 공리를 설치해야 합니다. Npm(노드 패키지 관리자)을 사용하여 이 작업을 수행할 것입니다(원하신다면 실과 같은 다른 패키지 관리자를 사용하셔도 됩니다).
아래 명령을 실행하여 프로젝트에 공리를 설치하겠습니다.
npm install axios
axios를 설치한 후에는 코드에 "axios" 키워드를 사용하기 전에 axios를 가져오는 것을 잊지 마십시오.
다음 코드줄을 사용하여 이 작업을 수행할 수 있습니다.
import axios from "axios";
요청 받기
요청 가져오기는 서버에서 데이터를 가져오기 위한 http 요청 유형입니다. React.js의 공리로 요청을 받는 방법이 있습니다.
import axios from "axios"; const [post, setPost] = React.useState(null); React.useEffect(() => { axios.get("PUT YOUR URL HERE").then((response) => { setPost(response.data); }); }, []); if (!post) return null; console.log(post); // This line of code is just for testing request
사후 요청
사후 요청은 데이터를 서버로 보내기 위한 http 요청 유형입니다. React.js의 공리로 사후 요청을 하는 방법이 있습니다.
import axios from "axios"; function postRequest() { const body = { fullname: "...", password: "...", }; const headers = { "Content-Type": "application/json", }; axios .post("PUT YOUR URL HERE", body, { headers }) .then((response) => console.log(response)); } /* note1: You can add different things from fullname and password to body. note2: You can use an onclick event to run postRequest function for testing request. */
그게 다예요. 도움이 되셨기를 바랍니다.
from http://it-ground.tistory.com/331 by ccl(A) rewrite - 2021-10-19 06:27:15