React 기준변수에 따라 레이블 변경하는 방법
React, Typescript의 클래스 컴퍼넌트 환경에서 monitoringType 변수가 T 일 경우 'Activity Name'를 리턴하고 S 일 경우
'Monitoring Name'를 리턴하는 예제 만들어 주세요.
import React, { Component } from 'react';
type MonitoringType = 'T' | 'S';
interface Props {
monitoringType: MonitoringType;
}
class MonitoringLabel extends Component
getLabel = (): string => {
const { monitoringType } = this.props;
switch (monitoringType) {
case 'T':
return 'Activity Name';
case 'S':
return 'Monitoring Name';
default:
return 'Unknown';
}
};
render() {
return (
<div>
<label>{this.getLabel()}</label>
</div>
);
}
}
export default MonitoringLabel;
switch 문장 말고 간단하게 T이면 Activity Name 나머지는 Monitoring Name 으로 간략하게 수정해주세요
import React, { Component } from 'react';
type MonitoringType = 'T' | 'S';
interface Props {
monitoringType: MonitoringType;
}
class MonitoringLabel extends Component
getLabel = (): string => {
return this.props.monitoringType === 'T' ? 'Activity Name' : 'Monitoring Name';
};
render() {
return (
<div>
<label>{this.getLabel()}</label>
</div>
);
}
}
export default MonitoringLabel;