From c4c0da057006bd379ebc5956944c9eec199651db Mon Sep 17 00:00:00 2001 From: Abbey Hart Date: Mon, 24 Feb 2020 18:26:01 -0600 Subject: [PATCH] refactor(with-state, structured-list, combobox): replace unsafe lifecycle methods (#5418) * refactor(components): replace unsafe lifecycle methods --- packages/react/src/components/ComboBox/ComboBox.js | 10 ++++------ .../src/components/StructuredList/StructuredList.js | 9 ++++++--- packages/react/src/tools/withState.js | 5 +++-- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/react/src/components/ComboBox/ComboBox.js b/packages/react/src/components/ComboBox/ComboBox.js index 5a2125c34a15..c9bd5fc818ca 100644 --- a/packages/react/src/components/ComboBox/ComboBox.js +++ b/packages/react/src/components/ComboBox/ComboBox.js @@ -187,6 +187,10 @@ export default class ComboBox extends React.Component { light: false, }; + static getDerivedStateFromProps(nextProps, state) { + return { inputValue: getInputValue(nextProps, state) }; + } + constructor(props) { super(props); @@ -199,12 +203,6 @@ export default class ComboBox extends React.Component { }; } - UNSAFE_componentWillReceiveProps(nextProps) { - this.setState(state => ({ - inputValue: getInputValue(nextProps, state), - })); - } - filterItems = (items, itemToString, inputValue) => items.filter(item => this.props.shouldFilterItem({ diff --git a/packages/react/src/components/StructuredList/StructuredList.js b/packages/react/src/components/StructuredList/StructuredList.js index cf331a783146..69d880980175 100644 --- a/packages/react/src/components/StructuredList/StructuredList.js +++ b/packages/react/src/components/StructuredList/StructuredList.js @@ -9,7 +9,7 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import { settings } from 'carbon-components'; -import uid from '../../tools/uniqueId'; +import setupGetInstanceId from '../../tools/setupGetInstanceId'; const { prefix } = settings; @@ -95,6 +95,8 @@ export class StructuredListHead extends Component { } } +const getInstanceId = setupGetInstanceId(); + export class StructuredListInput extends Component { static propTypes = { /** @@ -139,8 +141,9 @@ export class StructuredListInput extends Component { title: 'title', }; - UNSAFE_componentWillMount() { - this.uid = this.props.id || uid(); + constructor(props) { + super(props); + this.uid = this.props.id || getInstanceId(); } render() { diff --git a/packages/react/src/tools/withState.js b/packages/react/src/tools/withState.js index 0c13441daed0..b42bfa6c81be 100644 --- a/packages/react/src/tools/withState.js +++ b/packages/react/src/tools/withState.js @@ -13,8 +13,9 @@ export default class WithState extends React.PureComponent { initialState: PropTypes.object, }; - UNSAFE_componentWillMount() { - this.setState(this.props.initialState); + constructor(props) { + super(props); + this.state = this.props.initialState; } boundSetState = (...args) => this.setState(...args);