From f918daa881f9d0d4053d8bed7403a4e647945794 Mon Sep 17 00:00:00 2001 From: FedeG Date: Fri, 4 Oct 2019 03:06:15 -0300 Subject: [PATCH] Remove UNSAFE_* lifecycle methods from TitleList component --- .../__snapshots__/index.test.jsx.snap | 2 +- .../front/src/components/TitleList/index.jsx | 105 +++++++----------- 2 files changed, 43 insertions(+), 64 deletions(-) diff --git a/eventol/front/src/components/TitleList/__snapshots__/index.test.jsx.snap b/eventol/front/src/components/TitleList/__snapshots__/index.test.jsx.snap index 551c6ced..aeaca75d 100644 --- a/eventol/front/src/components/TitleList/__snapshots__/index.test.jsx.snap +++ b/eventol/front/src/components/TitleList/__snapshots__/index.test.jsx.snap @@ -145,7 +145,7 @@ exports[`TitleList Default without event data in request should be null when sho exports[`TitleList Default without event data in request should be show empty event when showEmpty is true 1`] = `
{ + const [data, setData] = useState({results: []}); + const [mounted, setMounted] = useState(false); + const {title, id, showEmpty, url} = props; - static defaultProps = { - showEmpty: false, - }; - - state = { - data: [], - mounted: false, - }; + const loadContent = useCallback(() => { + const fullUrl = getApiFullUrl(url); + getUrl(fullUrl).then(setData); + }, [url, setData]); - componentDidMount() { - const {url} = this.props; + useEffect(() => { if (!_.isEmpty(url)) { - this.loadContent(); - - // eslint-disable-next-line react/no-did-mount-set-state - this.setState({mounted: true}); - } - } - - componentWillReceiveProps({url}) { - const {url: prevUrl} = this.props; - if (!_.isEqual(url, prevUrl) && !_.isEmpty(url)) { - this.setState({mounted: true}, () => { - this.loadContent(); - }); + setMounted(true); + loadContent(); } - } + }, [loadContent, setMounted, url]); - loadContent() { - const {url} = this.props; - const fullUrl = getApiFullUrl(url); - getUrl(fullUrl).then(data => this.setState({data})); + const {results} = data; + let itemsData = ''; + if (results) { + itemsData = results.map(parseEventToItem); } - - render() { - const {title, id, showEmpty} = this.props; - const { - mounted, - data: {results}, - } = this.state; - let itemsData = ''; - if (results) { - itemsData = results.map(parseEventToItem); - } - if (_.isEmpty(itemsData)) { - if (!showEmpty) return null; - return ( -
-
-

{title}

- -
-
- ); - } + if (_.isEmpty(itemsData)) { + if (!showEmpty) return null; return (

{title}

- +
); } -} + return ( +
+
+

{title}

+ +
+
+ ); +}; + +TitleList.propTypes = { + id: PropTypes.string.isRequired, + showEmpty: PropTypes.bool, + title: PropTypes.string.isRequired, + url: PropTypes.string.isRequired, +}; + +TitleList.defaultProps = { + showEmpty: false, +}; + +export default TitleList;