【发布时间】:2022-01-06 22:56:27
【问题描述】:
我正在尝试将以下内容重构为 ES6 作为功能组件。我考虑研究 lodash 装饰器:https://www.npmjs.com/package/lodash-decorators#bind,但是有没有关于将这样的类组件重构为符合 ES6 的功能组件的技巧,尤其是当生命周期组件方法中的逻辑变得更加复杂时?
constructor(props) {
super(props);
this._isMounted = false;
this.state = {
profilePictureFailedToLoad: false,
};
if (process.browser) {
this.profileImage = new Image();
this.profileImage.src = this.props.userInfo.userAssetUrl;
}
this.handleLoginClick = this.handleLoginClick.bind(this);
this.handleLogoutClick = this.handleLogoutClick.bind(this);
this.handleProfileClick = this.handleProfileClick.bind(this);
this.handleModalClose = this.handleModalClose.bind(this);
this.handleDeleteAccountClick = this.handleDeleteAccountClick.bind(this);
this.handleDeleteAccountModalClose = this.handleDeleteAccountModalClose.bind(this);
}
componentDidMount() {
this._isMounted = true;
}
componentDidUpdate(prevProps) {
if (this.props.router.query.login === "true" && prevProps.router.query.login !== "true") {
console.log("Updating...")
}
}
componentWillUnmount() {
this._isMounted = false;
}
【问题讨论】:
标签: reactjs typescript refactoring