const [isLoading, setIsLoading] = useState(true); // 로딩 상태 추적
const [error, setError] = useState(null); // 에러 상태 추적
useEffect(() => {
const hash = window.location.hash;
const params = new URLSearchParams(hash.substring(1)); // # 뒤의 값을 추출
const accessToken = params.get('access_token'); // 토큰을 추출
if (accessToken) {
console.log('Access Token:', accessToken);
// 사용자 정보를 요청하는 API 호출
fetch('https://www.googleapis.com/oauth2/v3/userinfo', {
method: 'GET',
headers: {
Authorization: `Bearer ${accessToken}`,
},
})
.then((response) => {
if (!response.ok) {
throw new Error('네트워크 응답이 좋지 않습니다.');
}
return response.json(); // JSON 형태로 응답 변환
})
.then((userInfo) => {
console.log('사용자 정보: ', userInfo);
console.log('사용자 이메일: ', userInfo.email);
console.log('사용자 이름: ', userInfo.name)
setIsLoading(false); // 데이터 로딩 완료 시 상태 변경
})
.catch((error) => {
console.error('사용자 정보 요청 실패:', error);
setError('사용자 정보 요청에 실패했습니다.'); // 에러 상태 처리
setIsLoading(false); // 로딩 완료 상태
});
navigate('/'); // 토큰 처리 후 원하는 페이지로 이동
} else {
setIsLoading(false); // 액세스 토큰이 없으면 로딩 완료 처리
setError('액세스 토큰이 없습니다.'); // 에러 처리
}
}, [navigate]);
3. document.querySelector() as Element : 웬만하면 쓰지 말것
4. if (제목?.innerHTML) : 제목이 있으면 출력, 없으면 undefined (if아니여도 인정해준다)
5. tsconfig.json파일 - "strictNullChecks": true 지우기 : 무식한 방법
* href를 바꿀 때, narrowing을 하려면 instanceof HTMLAnchorElement 사용
그 외 HTMLHeadingElement, HTMLButtonElement 등이 있다
TypeScript class 키워드(JS 문법)
<script>
class Hero{
constructor(구멍){
this.q = 구멍;
this.w = 'snowball';
}
}
var nunu = new Hero("consume")
var garen = new Hero("strike")
</script>
TypeScript prototype 문법(JS 문법)
prototype : 유전자
해당 클래스를 상속받은 객체들은 해당 클래스의 prototype을 설정해주면 모두 그 값을 받아올 수 있다
<script>
class 기계{
constructor(){
this.q = "strike";
this.w = 'snowball';
}
}
기계.prototype.name = 'kim';
var nunu = new 기계();
var garen = new 기계();
var 어레이 = [4, 2, 1];
var 어레이 = new Array(4, 2, 1);
Array.prototype.함수 = function(){}
</script>
TypeScript class 타입지정
constructor()는 필드값에 변수가 미리 있어야 this.변수명 이 가능하다
class Person{
name :string;
constructor(a :string){
this.name = a;
}
함수(a :string){
console.log('안녕' + a);
}
}
let 사람1 = new Person("kim");
let 사람2 = new Person("park");
사람1.함수("안녕");
TypeScript interface
object 타입지정시 interface 사용가능
interface의 장점 : extends로 복사가능
interface Student {
name : string
}
interface Teacher extends Student{
age : number
}
let 학생 :Student = {name : 'kim'}
let 선생 :Teacher = {name : 'kim', age : 20}
type alias도 &로 extend기능 사용 가능
type Animal = {
name : string
}
type Cat = {
age : number
} & Animal
let 고양이 :Cat = {name : 'kim', age : 20}
type vs inteface
interface는 중복선언 가능
type은 중복선언 불가능
interface Student {
name : string
}
interface Student {
score : number
}
interface Teacher extends Student{
age : number
}
let 학생 :Student = {name : 'kim', score : 23}
let 선생 :Teacher = {name : 'kim', age : 20, score : 100}