A ? A : B
와 같이 사용하는 것을 A ?? B
로 작성할 수 있습니다.A
를 두 번 반복하여 적는 경우가 많았는데 ??
연산자로 보다 직관적이면서 짧게 작성할 수 있습니다.||
연산자 : 첫 번째 truthy 값을 반환합니다.??
연산자 : 첫 번째 정의된(defined) 값을 반환합니다.const height = 0;
console.log(height || 100); // 100
console.log(height ?? 100); // 0
참조 : JAVASCRIPT.iNFO
|
연사자 사용하여 하나의 타입명으로 받을 경우 사용합니다.import { NativeStackScreenProps } from "@react-navigation/native-stack";
import React, { useEffect } from "react";
import styled from "styled-components/native";
import Poster from "../components/Poster";
import { Movie, Tv } from "../interfaces";
type RootStackParamList = {
Detail: Movie | Tv;
};
type DetailScreenProps = NativeStackScreenProps<RootStackParamList, "Detail">;
const Container = styled.ScrollView`
background-color: ${(props) => props.theme.mainBgColor};
`;
const Detail: React.FC<DetailScreenProps> = ({
navigation: { setOptions },
route: { params },
}) => {
useEffect(() => {
setOptions({
title:
"original_title" in params
? params.original_title
: params.original_name,
});
}, []);
return (
<Container>
<Poster poster_path={params.poster_path || ""} />
</Container>
);
};
export default Detail;