concept UI in category react native

This is an excerpt from Manning's book React Native in Action.
At the top of the file, you require
Reactfrom'react', as well asView,Text, andStyleSheetfrom'react-native'.Viewis the most fundamental building block for creating React Native components and the UI in general and can be thought of like adivin HTML.Textallows you to create text elements and is comparable to aspantag in HTML.StyleSheetlets you create style objects to use in an application. These two packages (reactandreact-native) are available as npm modules.
Figure 2.1 shows how the state is updated each time the text element in listing 2.3 is pressed. Every time
setStateis called, React will rerender the component (calling therendermethod again) and any child components. Callingthis.setStateis the way to change a state variable and trigger therendermethod again, because changing the state variable directly won’t trigger a rerender of the component and therefore no changes will be visible in the UI. A common mistake for beginners is updating the state variable directly. For example, something like the following doesn’t work when trying to update state—thestateobject is updated, but the UI isn’t updated becausesetStateisn’t called and the component isn’t rerendered:class MyComponent extends Component { constructor(){ super() this.state = { year: 2016, } } updateYear() { this.state.year = 2017 } render() { return ( <View> <Text onPress={() => this.updateYear()}> The year is: { this.state.year } </Text> </View> ) } }But a method is available in React that can force an update once a state variable has been changed as in the previous snippet. This method is called
forceUpdate; see listing 2.4. CallingforceUpdatecausesrenderto be called on the component, triggering a rerendering of the UI. UsingforceUpdateisn’t usually necessary or recommended, but it’s good to know about in case you run into it in examples or documentation. Most of the time, this rerendering can be handled using other methods such as callingsetStateor passing in new props.
Figure 10.4 Rendering
ProgressViewIOSin the UI![]()