use constructor to initialize component state

This commit is contained in:
Yoshihiro OKUMURA
2019-05-13 17:16:16 +09:00
parent 49ab6e011d
commit cf4aef5dab
7 changed files with 33 additions and 70 deletions
+6 -10
View File
@@ -16,13 +16,6 @@ interface State {
class IndexTreeBlock extends Component<Props, State> {
public state: State = {
tree: [],
expandableKeys: [],
expandedKeys: [],
selectedKeys: [],
};
constructor(props: Props) {
super(props);
this.handleClickOpenAll = this.handleClickOpenAll.bind(this);
@@ -30,9 +23,12 @@ class IndexTreeBlock extends Component<Props, State> {
this.handleExpand = this.handleExpand.bind(this);
this.handleSelect = this.handleSelect.bind(this);
const res = this.load();
this.state.tree = res.elements;
this.state.expandableKeys = res.keys;
this.state.expandedKeys = res.keys;
this.state = {
tree: res.elements,
expandableKeys: res.keys,
expandedKeys: res.keys,
selectedKeys: [],
};
}
load() {
+2 -10
View File
@@ -12,16 +12,10 @@ interface State {
class SearchBlock extends Component<Props, State> {
public state: State = {
type: 'all',
keyword: ''
}
constructor(props: Props) {
super(props);
const { type, keyword } = ItemUtil.getSearchKeywordByQuery(this.props.location.search);
this.state.type = type;
this.state.keyword = keyword;
this.state = { type, keyword };
this.handleChangeType = this.handleChangeType.bind(this);
this.handleChangeKeyword = this.handleChangeKeyword.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
@@ -45,9 +39,7 @@ class SearchBlock extends Component<Props, State> {
handleSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
const url = ItemUtil.getSearchByKeywordUrl(this.state.type, this.state.keyword);
if (this.state.keyword.length > 0) {
this.props.history.push(url);
}
this.props.history.push(url);
}
render() {
@@ -14,15 +14,13 @@ interface State {
class DatabaseSearchByIndexId extends Component<Props, State> {
public state: State = {
index: IndexUtil.get(INDEX_ID_PUBLIC),
};
constructor(props: Props) {
super(props);
const { params } = this.props.match;
const indexId = params.id ? parseInt(params.id, 10) : INDEX_ID_PUBLIC;
this.state.index = IndexUtil.get(indexId);
this.state = {
index: IndexUtil.get(indexId),
}
this.search = this.search.bind(this);
}
@@ -12,16 +12,13 @@ interface State {
class DatabaseSearchByItemType extends Component<Props, State> {
public state: State = {
item_type: '',
sub_item_type: '',
};
constructor(props: Props) {
super(props);
const { params } = this.props.match;
this.state.item_type = params.itemType ? params.itemType : '';
this.state.sub_item_type = params.subItemType ? params.subItemType : '';
this.state = {
item_type: params.itemType ? params.itemType : '',
sub_item_type: params.subItemType ? params.subItemType : '',
};
this.search = this.search.bind(this);
}
@@ -12,16 +12,10 @@ interface State {
class DatabaseSearchByKeyword extends Component<Props, State> {
public state: State = {
type: 'all',
keyword: '',
};
constructor(props: Props) {
super(props);
const { type, keyword } = ItemUtil.getSearchKeywordByQuery(this.props.location.search);
this.state.type = type;
this.state.keyword = keyword;
this.state = {type, keyword};
this.search = this.search.bind(this);
}
@@ -22,23 +22,16 @@ interface State {
class FileDownloadButton extends Component<Props, State> {
public state: State = {
file: null,
rights: '',
useCc: 0,
ccCommercialUse: 0,
ccModification: 0,
show: false,
};
constructor(props: Props) {
super(props);
this.state.file = this.props.file;
this.state.rights = this.props.rights;
this.state.useCc = this.props.useCc;
this.state.ccCommercialUse = this.props.ccCommercialUse;
this.state.ccModification = this.props.ccModification;
this.state.show = false;
this.state = {
file: this.props.file,
rights: this.props.rights,
useCc: this.props.useCc,
ccCommercialUse: this.props.ccCommercialUse,
ccModification: this.props.ccModification,
show: false,
};
this.handleClickDownload = this.handleClickDownload.bind(this);
}
@@ -28,24 +28,17 @@ interface State {
class LicenseAgreementDialog extends Component<Props, State> {
public state: State = {
file: null,
rights: '',
useCc: 0,
ccCommercialUse: 0,
ccModification: 0,
show: false,
disabled: false,
};
constructor(props: Props) {
super(props);
this.state.file = this.props.file;
this.state.rights = this.props.rights;
this.state.useCc = this.props.useCc;
this.state.ccCommercialUse = this.props.ccCommercialUse;
this.state.ccModification = this.props.ccModification;
this.state.show = this.props.show;
this.state = {
file: this.props.file,
rights: this.props.rights,
useCc: this.props.useCc,
ccCommercialUse: this.props.ccCommercialUse,
ccModification: this.props.ccModification,
show: this.props.show,
disabled: false,
};
this.handleChangeCheckbox = this.handleChangeCheckbox.bind(this);
this.handleClickDownload = this.handleClickDownload.bind(this);
this.handleClickCancel = this.handleClickCancel.bind(this);