ElemeFE / ElemeFE/element-react

Select component with no options throws error when life cycle enters componentWillUnmount

Open
#781 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
2.8k
Forks
435
PR merge metrics
No merged PRs in 30d

Description

### Description

Select component with no select options throws error when calling `removeResizeListener` in `ComponentWillUnmount`.

I noticed that `addResizeListener(this.refs.root, this.resetInputWidth);` has been moved to `ComponentDidUpdate` in recent code commits(it was written in `componentDidMount` before). If users do nothing , `componentDidUpdate` will be never called, i.e. ` addResizeListener(this.refs.root, this.resetInputWidth);` will not be called, which results in throwing error if Select with empty children option components are unmounted.

Here are the new source codes:

Select.jsx
```
componentDidUpdate() {
this.state.inputWidth = this.reference.getBoundingClientRect().width;
addResizeListener(this.refs.root, this.resetInputWidth);
}

componentWillUnmount() {
removeResizeListener(this.refs.root, this.resetInputWidth);
}
```

### Solution

If select has no options, it is required to push an empty object into select options array, but I don't think it is the best practice for component usage

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.