• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 React 게시판 usehistory() invalid hook call 오류입니다.

usehistory() invalid hook call 오류입니다.

5 글 보임 - 1 에서 5 까지 (총 5 중에서)
  • 글쓴이
  • #31888

    고진규
    참가자

    import axios from "axios";
    import { useHistory } from "react-router-dom";
    function Logging(){
    const username = document.getElementById("username");
    const password = document.getElementById('password');
     
    let history = useHistory();
    //history.push('/Translation');
    axios({
    method:"POST",
    url: '~~~~~~~~~~',
    data:{
    "username": username?.value,
    "password": password?.value
    }
    }).then((res)=>{//성공
    console.log(res);
    console.log(res.data.refresh_token);
    localStorage.setItem("re-token",res.data.refresh_token);
    localStorage.setItem("ac-token",res.data.access_token);
    window.alert(JSON.stringify(res.data.username).replace(/\"/gi, "")+"님 접속 성공");
     
    }).catch(error=>{
    console.log(error);
    window.alert("접속실패.");
    });
    }
    export default Logging;

     

    이런식으로하고있는데

    let history = useHistory(); 이것만 적으면 invalid hook call 오류가 생기더라고요,,왜이럴까요..?

     

    // eslint-disable
    import './App.css';
    import 'bootstrap/dist/css/bootstrap.min.css';
    import { Link,Route,Switch} from 'react-router-dom';
    import React ,{useState}from 'react';
    import Translation from './Translation.js';
    import Login from './Login.js';
    import Main from './Main.js';
    import Sg from './Sg';
    import { BrowserRouter } from 'react-router-dom';

    function App() {
    return (
    <div>
    <BrowserRouter>
    <Route exact path='/' >
    <Main/>
    </Route>
    <Route exact path='/sg' >
    <Sg/>
    </Route>
    <Route exact path='/Login' >
    <Login/>
    </Route>
    <Route exact path='/Translation' >
    <Translation/>
    </Route>
    </BrowserRouter>
    </div>
    );
    }

    export default App;

    #31889

    고진규
    참가자

    "dependencies": {
    "@fortawesome/fontawesome-svg-core": "^1.3.0",
    "@fortawesome/free-solid-svg-icons": "^6.0.0",
    "@fortawesome/react-fontawesome": "^0.1.17",
    "@testing-library/jest-dom": "^5.16.2",
    "@testing-library/react": "^12.1.3",
    "@testing-library/user-event": "^13.5.0",
    "axios": "^0.26.1",
    "bootstrap": "^5.1.3",
    "express": "^4.17.3",
    "gapi-script": "^1.2.0",
    "google-auth-library": "^7.14.1",
    "mysql": "^2.18.1",
    "react": "^17.0.2",
    "react-bootstrap": "^2.1.2",
    "react-cookie": "^4.1.1",
    "react-dom": "^17.0.2",
    "react-feather": "^2.0.9",
    "react-google-login": "^5.2.2",
    "react-icons": "^4.3.1",
    "react-router-dom": "^5.3.0",
    "react-scripts": "5.0.0",
    "web-vitals": "^2.1.4"
    },

    이정보도 필요할까봐,..적었습니다..

    #31894

    codingapple
    키 마스터

    보통 라이브러리 설치가 잘못된 경우가 많습니다 새로운 프로젝트에 설치해봅시다 

    지금 서버프로젝트랑 같은 폴더에 리액트 설치해서 쓰는거같은데

    프론트랑 백엔드는 다른 폴더에 개발하는게 좋습니다

    #31897

    고진규
    참가자

    코드상으로는 문제가없는건가요??

    history말고 로그인이 됐을시 페이지이동 할만한 다른게 있을까요..?

     

    #31903

    codingapple
    키 마스터

    네 useHistory 하나만 썼는데 그러면 대부분 패키지 설치문제입니다

5 글 보임 - 1 에서 5 까지 (총 5 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관, 개인정보처리방침
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠