From 1e0009407d6f96c255191b85628699f4e77a3d08 Mon Sep 17 00:00:00 2001 From: Fabrizio Cucci Date: Mon, 13 Jan 2025 08:59:43 -0800 Subject: [PATCH] Migrate rn-tester/js/components/TextInlineView.js to function components (#48640) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48640 As per title. Changelog: [Internal] Reviewed By: christophpurrer Differential Revision: D68095013 fbshipit-source-id: a593af63029352844e544245d88e7b4c9d7eb75c --- .../rn-tester/js/components/TextInlineView.js | 184 ++++++++---------- 1 file changed, 83 insertions(+), 101 deletions(-) diff --git a/packages/rn-tester/js/components/TextInlineView.js b/packages/rn-tester/js/components/TextInlineView.js index 360a15a17cb140..c7e884ae2250a4 100644 --- a/packages/rn-tester/js/components/TextInlineView.js +++ b/packages/rn-tester/js/components/TextInlineView.js @@ -12,6 +12,7 @@ import RNTesterText from '../components/RNTesterText'; import React from 'react'; +import {useState} from 'react'; import {Image, TouchableHighlight, View} from 'react-native'; function Basic(): React.Node { @@ -105,116 +106,97 @@ function ClippedByText(): React.Node { ); } -type ChangeSizeState = {| - width: number, -|}; - -class ChangeImageSize extends React.Component { - state: ChangeSizeState = { - width: 50, - }; - - render(): React.Node { - return ( - - { - this.setState({width: this.state.width === 50 ? 100 : 50}); - }}> - - Change Image Width (width={this.state.width}) - - - - This is an - - inline image +function ChangeImageSize(): React.Node { + const [width, setWidth] = useState(50); + return ( + + { + setWidth(width === 50 ? 100 : 50); + }}> + + Change Image Width (width={width}) - - ); - } + + + This is an + + inline image + + + ); } -class ChangeViewSize extends React.Component { - state: ChangeSizeState = { - width: 50, - }; +function ChangeViewSize(): React.Node { + const [width, setWidth] = useState(50); + return ( + + { + setWidth(width === 50 ? 100 : 50); + }}> + + Change View Width (width={width}) + + + + This is an + + inline view + + + ); +} - render(): React.Node { - return ( - - { - this.setState({width: this.state.width === 50 ? 100 : 50}); - }}> - - Change View Width (width={this.state.width}) - - - - This is an +function ChangeInnerViewSize(): React.Node { + const [width, setWidth] = useState(50); + return ( + + { + setWidth(width === 50 ? 100 : 50); + }}> + {/* When updating `width`, it's important that the only thing that + changes is the width of the pink inline view. When we do this, we + demonstrate a bug in RN Android where the pink view doesn't get + rerendered and remains at its old size. If other things change + (e.g. we display `width` as text somewhere) it could circumvent + the bug and cause the pink view to be rerendered at its new size. */} + + Change Pink View Width + + + + This is an + - inline view - - - ); - } -} - -class ChangeInnerViewSize extends React.Component { - state: ChangeSizeState = { - width: 50, - }; - - render(): React.Node { - return ( - - { - this.setState({width: this.state.width === 50 ? 100 : 50}); - }}> - {/* When updating `state.width`, it's important that the only thing that - changes is the width of the pink inline view. When we do this, we - demonstrate a bug in RN Android where the pink view doesn't get - rerendered and remains at its old size. If other things change - (e.g. we display `state.width` as text somewhere) it could circumvent - the bug and cause the pink view to be rerendered at its new size. */} - - Change Pink View Width - - - - This is an - - - - inline view - - - ); - } + + inline view + + + ); } module.exports = {