Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 프로그래밍첫걸음시작하기
- ionic-react
- next.js 빌드 오류
- 바이트디그리
- react
- Firebase
- 부트캠프
- 알고리즘
- 항해99
- React Native
- ionic react
- PYTHON
- react-native
- 프로그래밍기초
- Algorithm
- spartacodingclub
- CSS
- K디지털크레딧
- ionic
- mongodb
- typescript
- styled-components
- HTML
- javascript
- styled components
- 프로그래머스
- 패스트캠퍼스
- 스파르타코딩클럽
- 내일배움카드
- 스파르타코딩클럽 후기
Archives
- Today
- Total
bravo my life!
[Ionic-React] TypeScript, styled-components 로 조건 변경시 CSS 바꿔주기 본문
개발기/[Travelit] Ionic-React
[Ionic-React] TypeScript, styled-components 로 조건 변경시 CSS 바꿔주기
losajjang 2022. 7. 17. 21:56728x90
1. 개요
boolean값으로 상태를 관리하며 어떤 아이콘을 false때는 상단에 빨간점을 표시하지 않고, true때는 상단에 빨간점을 표시하는 기능을 만들어 볼 것이다.
2. 코드 상세
//HomeHeader.tsx
import { useState } from 'react';
import styled from 'styled-components';
//인터페이스를 사용하여 NotiState의 타입을 지정해준다.
interface NotiState {
noti: boolean;
}
const NotificationButtons = styled(IonButtons)`
margin-right: 20px;
`;
//noti의 boolean 상태값에 따라 백그라운드 이미지를 바꾸어 줄 것이다.
const NotificationButton = styled(IonButton)<NotiState>`
background-image: ${({ noti }) =>
(noti
? "url('/assets/icon/notification_notConfirm.png')"
: "url('/assets/icon/notification_confirmed.png')")};
background-size: 24px;
background-repeat: no-repeat;
background-position: center;
width: 24px;
`;
const HomeHeader = () => {
// 알림이 있다면 setNoti(true)로 알림 아이콘을 바꾸어 준다.
const [noti, setNoti] = useState<boolean>(false);
return (
<IonHeader>
<IonToolbar>
<NotificationButtons slot="end">
<NotificationButton
fill="clear"
noti={noti}
routerLink="/noti"
></NotificationButton>
</NotificationButtons>
</IonToolbar>
</IonHeader>
);
};
export default HomeHeader;
코드의 결과물은 다음과 같다.
좌측은 noti의 상태가 false일 경우, 우측은 noti의 상태가 true일 경우이다.
'개발기 > [Travelit] Ionic-React' 카테고리의 다른 글
[Ionic-React][중간기록] 마이페이지 프로필 레이아웃 완성 (0) | 2022.08.02 |
---|---|
[Ionic-React] 중간 기록. 홈 UI 완성. (0) | 2022.07.22 |
[Ionic-React] CSS 부모의 패딩 속성 무시하기 (0) | 2022.07.22 |
[Ionic-React] Swiperjs의 scrollbar를 커스텀 하기(feat. 색상, 크기) (0) | 2022.07.22 |
[Ionic-React] 코드의 간결성. 버릴건 버리자. (0) | 2022.07.17 |